Spring Boot 嵌入式Servlet容器启动原理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Java_Glory/article/details/90023322

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

2,refreshContext(context);Spring Boot刷新IOC容器【创建IOC容器对象,并初始化容器,创建容器中的每一个组件】;如果是web应用创建一个web的ioc容器: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 = this.getEmbeddedServletContainerFactory();

          从ioc容器中获取 EmbeddedServletContainerFactory 组件;TomcatEmbeddedServletContainerFactory 创建对象,后置处理器发现是这个对象,就获取所有的定制器来先定制Servlet容器的相关配置;

7,使用容器工厂获取嵌入式的 Servlet 容器:

this.embeddedServletContainer = containerFactory.getEmbeddedServletContainer(new ServletContextInitializer[]{this.getSelfInitializer()});

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

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

可以结合 Spring Boot 中嵌入式 Servlet 容器自动配置原理 浏览。

猜你喜欢

转载自blog.csdn.net/Java_Glory/article/details/90023322