49.SpringBoot学习笔记

嵌入式Servlet容器启动原理

Posted by Chen Xingxu on June 20, 2020

49.SpringBoot学习笔记–嵌入式Servlet容器启动原理

嵌入式 Servlet 容器启动原理

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

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

1)Spring Boot 应用启动,运行 run 方法;

2)refreshContext(context):Spring Boot 刷新 IOC 容器【创建 IOC 容器对象,并初始化容器,创建容器中的每一个组件】。如果是 Web 应用创建 AnnotationConfigEmbeddedWebApplicationContext,否则:AnnotationConfigApplicationContext

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)Web 的 IOC 容器会创建嵌入式的 Servlet 容器:createEmbeddedServletContainer()

6)获取嵌入式的 Servlet 容器工厂

EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();

从 IOC 容器中获取 EmbeddedServletContainerFactory 组件。TomcatEmbeddedServletContainerFactory创建对象,后置处理器一看是这个对象,就获取所有的定制器来定制 Servlet 容器的相关配置

7)使用容器工厂获取嵌入式的 Servlet 容器:this.embeddedServletContainer = containerFactory .getEmbeddedServletContainer(getSelfInitializer());

8)嵌入式的 Servlet 容器创建对象并启动 Servlet 容器

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

总结:IOC 容器启动创建嵌入式的 Servlet 容器。