• 4_8.springboot2.x嵌入式servlet容器启动原理解析


    问题描述:
    什么时候创建嵌入式的Servlet容器工厂?
    什么时候获取嵌入式的Servlet容器并启动Tomcat?

    *获取嵌入式的Servlet容器工厂:

    1)、SpringBoot应用启动运行run方法在这里插入图片描述
    2)、refreshContext(context);SpringBoot刷新IOC容器【创建IOC容器对象,并初始化容器,创建容器中的每一个组件】;如果是web应用创建AnnotationConfigServletWebServerApplicationContext,否则:AnnotationConfigApplicationContext

    a、创建容器并且判断:
    在这里插入图片描述
    刷新容器:
    在这里插入图片描述
    3)、refresh(context);刷新刚才创建好的ioc容器;

    public void refresh() throws BeansException, IllegalStateException {
    		synchronized (this.startupShutdownMonitor) {
    			// Prepare this context for refreshing.
    			prepareRefresh();
    
    			// Tell the subclass to refresh the internal bean factory.
    			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    
    			// Prepare the bean factory for use in this context.
    			prepareBeanFactory(beanFactory);
    
    			try {
    				// Allows post-processing of the bean factory in context subclasses.
    				postProcessBeanFactory(beanFactory);
    
    				// Invoke factory processors registered as beans in the context.
    				invokeBeanFactoryPostProcessors(beanFactory);
    
    				// Register bean processors that intercept bean creation.
    				registerBeanPostProcessors(beanFactory);
    
    				// Initialize message source for this context.
    				initMessageSource();
    
    				// Initialize event multicaster for this context.
    				initApplicationEventMulticaster();
    
    				// Initialize other special beans in specific context subclasses.
    				onRefresh();
    
    				// Check for listener beans and register them.
    				registerListeners();
    
    				// Instantiate all remaining (non-lazy-init) singletons.
    				finishBeanFactoryInitialization(beanFactory);
    
    				// Last step: publish corresponding event.
    				finishRefresh();
    			}
    
    			catch (BeansException ex) {
    				if (logger.isWarnEnabled()) {
    					logger.warn("Exception encountered during context initialization - " +
    							"cancelling refresh attempt: " + ex);
    				}
    
    				// Destroy already created singletons to avoid dangling resources.
    				destroyBeans();
    
    				// Reset 'active' flag.
    				cancelRefresh(ex);
    
    				// Propagate exception to caller.
    				throw ex;
    			}
    
    			finally {
    				// Reset common introspection caches in Spring's core, since we
    				// might not ever need metadata for singleton beans anymore...
    				resetCommonCaches();
    			}
    		}
    	}
    

    4)、 onRefresh(); web的ioc容器重写了onRefresh方法
    5)、webioc容器会创建嵌入式的Servlet容器;** createWebServer();
    6)、获取嵌入式的Servlet容器工厂
    ServletWebServerFactory factory = getWebServerFactory();
    从ioc容器中获取
    ServletWebServerFactory**组件;
    tomcatServletWebServerFactory创建对象,后置处理器一看是这个对象,就获取所有的定制器来先定制Servlet容器的相关配置;
    7)、使用容器工厂获取嵌入式的Servlet容器
    this.webServer = factory.getWebServer(getSelfInitializer());
    8)、嵌入式的Servlet容器创建对象并启动Servlet容器;

    先启动嵌入式的Servlet容器,再将ioc容器中剩下没有创建出的对象获取出来;

    IOC容器启动创建嵌入式的Servlet容器

  • 相关阅读:
    Docker入门(windows版),利用Docker创建一个Hello World的web项目
    SpringBoot集成JWT实现token验证
    Jedis的基本操作
    Java动态代理详解
    SpringBoot利用自定义注解实现通用的JWT校验方案
    递归——汉诺塔问题(python实现)
    Datatable删除行的Delete和Remove方法的区别
    C# DEV使用心得
    总结
    安装插件时
  • 原文地址:https://www.cnblogs.com/jatpeo/p/11767501.html
Copyright © 2020-2023  润新知