SpringBoot源码之SpringApplication.run启动流程

  • SpringApplication初始化

         

设置初始化器列表:多个classpath*:META-INF/spring.factories中的属性KEY为                        org.springframework.context.ApplicationContextInitializer的属性的值。

               

         设置监听器列表:多个classpath*:META-INF/spring.factories中的属性KEY为                        org.springframework.context.ApplicationListener的属性的值。

                

  • 调用SpringApplication的run方法

1.创建SpringApplicationRunListeners,并调用starting()

2.准备Environment

  • 获取或创建Environment,WEB项目返回StandardServletEnvironment
  • 配置Environment
  • 发布ApplicationEnvironmentPreparedEvent事件

3.打印Banner

4创建应用上下文AnnotationConfigEmbeddedWebApplicationContext

AnnotationConfigEmbeddedWebApplicationContext:

接受注解类作为输入-特别是@Configuration,@Component和依从JSR-330使用 {@code javax.inject}注解的类。允许逐个注册类(指定类名作为配置位置)以及类路径扫描(指定基本包作为配置位置)。对于多个{@code@Configuration}类,后面的{@code@Bean}定义将覆盖先前加载文件中定义的定义。可以通过额外配置类来有意的覆盖某些bean定义。

扫描二维码关注公众号,回复: 4457987 查看本文章

EmbeddedWebApplicationContext:

此上下文会通过搜索一个{@link EmbeddedServletContainerFactory}bean,创建,实例化,运行一个{@link EmbeddedServletContainer}

此外,在上下文中定义的任何{@link Servlet}或{@link Filter}bean将自动注册到嵌入的Servlet容器。在单个servlet bean的情况下,将使用'/'映射。如果找到多个servlet bean,那么小写bean名称将被用作映射前缀。任何一个名为“dispatcherServlet”的servlet都将被映射到'/'。过滤器bean将被映射到所有URL(‘/*’)。

对于更高级的配置,上下文可以替代定义实现{@link ServletContextInitializer}接口的bean。为了防止双重注册,使用{@link ServletContextInitializer}bean将禁用自动Servlet和Filter bean注册。虽然这个上下文可以直接使用,大多数开发者应该考虑使用{@link AnnotationConfigEmbeddedWebApplicationContext} or {@link XmlEmbeddedWebApplicationContext} 。

GenericWebApplicationContext:

实现{@link org.springframework.web.context.ConfigurableWebApplicationContext},但不用于{@code web.xml}中的声明性设置。相反,它是为程序设置而设计的。例如:构建嵌套上下文 或者 为了在WebApplicationInitializer中使用。

如果你想实现一个从配置文件中读取BEAN定义的WebApplicationContext ,考虑从AbstractRefreshableWebApplicationContext读取BEAN定义,通过loadBeanDefinitions方法的一个实现 。

将资源路径解释为servlet上下文资源,即作为Web应用程序根下的路径。绝对路径,例如web应用程序根目录之外的文件,可以通过“file:”URL访问,如AbstractApplicationContext所实现的。除了由{@link org.springframework.context.support.AbstractApplicationContext}检测到的特殊bean之外,这个类在上下文中检测ThemeSource bean,名为“themeSource”。

GenericApplicationContext:

通用的ApplicationContext实现,持有一个内部的DefaultListableBeanFactory实例而且不承担特定的bean定义格式。实现{@link org.springframework.beans.factory.support.BeanDefinitionRegistry}接口,以便允许对它应用任何bean定义读取器。典型的用法是通过{@link org.springframework.beans.factory.support.BeanDefinitionRegistry}接口注册各种bean定义,然后调用{@link #refresh()} 来用应用程序上下文语义初始化这些bean。与为每个刷新创建新的内部bean工厂实例的其他应用程序上下文实现相反,此上下文的内部bean工厂可以从一开始就可用,以便能够在其上注册bean定义。{@link #refresh()}只能调用一次。例如 : 

 * GenericApplicationContext ctx = new GenericApplicationContext();
 * XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
 * xmlReader.loadBeanDefinitions(new ClassPathResource("applicationContext.xml"));
 * PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(ctx);
 * propReader.loadBeanDefinitions(new ClassPathResource("otherBeans.properties"));
 * ctx.refresh();
 *
 * MyBean myBean = (MyBean) ctx.getBean("myBean");

5.准备上下文,实际上是把MAIN方法启动类的BEAN定义注册到BEAN工厂的MAP中。

6.刷新上下文

  • prepareRefresh   准备此上下文以进行刷新。
  • obtainFreshBeanFactory  告诉子类刷新内部bean工厂。
  • prepareBeanFactory准备此BEAN工厂以进行使用。会注册BEAN environment 到 单例BEAN注册器???
  • postProcessBeanFactory允许在上下文子类中对bean工厂进行后处理。添加BEAN后置处理器WebApplicationContextServletContextAwareProcessor,忽略依赖的ServletContextAware接口。
  • invokeBeanFactoryPostProcessors 工厂后置处理器在上下文中注册为bean。invokeBeanDefinitionRegistryPostProcessors,发现候选的组件(即:启动类路径下的最终由@Component注解的类,注意,@Controller @Service  @Configuration注解中包含@Component注解),将其初始化为BeanDefinitionHolder,并将其BEAN定义注册到BEAN工厂的beanDefinitionMap
  • 检查任何其他配置类的扫描定义集,并在需要时递归解析

          处理@ComponentScan注解

          处理@Import注解  启动类有扫描到AutoConfigurationPackages$Registrar, EnableAutoConfigurationImportSelector,

ParserStrategyUtils.invokeAwareMethods调用AwareMethods,即:此Registrar/ImportSelector实现的Aware接口的Aware方法。

新增了一个DeferredImportSelectorHolder,在后面调用。

          处理@ImportResource注解

          处理独特的@Bean methods  

          处理接口的默认方法

          处理父CLASS

          处理延期的ImportSelectors即:从多个classpath*:META-INF/spring.factories中的属性KEY为                        org.springframework.boot.autoconfigure.EnableAutoConfiguration的属性的值。去重,排序,去除exclusions:即spring.autoconfigure.exclude的值,过滤,只引入JPA的情况下,会返回以下配置类:

org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration, 
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration, 
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration, 
org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration, 
org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration, 
org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration, 
org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration, 
org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration, 
org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration, 
org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration, 
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration, 
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration, 
org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration, 
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration, 
org.springframework.boot.autoconfigure.jdbc.XADataSourceAutoConfiguration, 
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, 
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, 
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, 
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration, 
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration, 
org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, 
org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration, 
org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration, 
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration, 
org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, 
org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, 
org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, 
org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration, 
org.springframework.boot.autoconfigure.web.HttpEncodingAutoConfiguration, 
org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration, 
org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration, 
org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration

然后,发布AutoConfigurationImportEvent,解析以上配置类中的@Bean注解的方法为BeanMethod。

以上配置类本身解析为BeanDefinition注册到BeanFactory,这的BeanMethod也解析为BeanDefinition注册到BeanFactory,调用 ImportBeanDefinitionRegistrar的registerBeanDefinitions方法注册BeanDefinition到BeanFactory

invokeBeanFactoryPostProcessors 新增ImportAwareBeanPostProcessor

5.注册bean处理器,拦截bean创建。

注册下列BEAN  :

ConfigurationPropertiesBindingPostProcessor
PersistenceAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
AutowiredAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor

ProxyTransactionManagementConfiguration BeanFactoryTransactionAttributeSourceAdvisor实例化,这个很重要,SPRING事物相关BEAN在此实例化,比较关键的类有:

       BeanFactoryTransactionAttributeSourceAdvisor

       TransactionAttributeSource AnnotationTransactionAttributeSource

       TransactionAttributeSourcePointcut

       SpringTransactionAnnotationParser

       TransactionInterceptor        TransactionAspectSupport

EmbeddedServletContainerCustomizerBeanPostProcessor
ErrorPageRegistrarBeanPostProcessor
DataSourceInitializedPublisher
ProjectingArgumentResolverBeanPostProcessor

6.初始化此上下文的消息源。

7.初始化此上下文的事件多播器。

8.在特定上下文子类中初始化其他特殊bean。创建内置的Servlet容器,初始化属性源。

8.检查listener BEAN,并注册它们。

9.实例化所有剩余的(非懒初始化)单例。

10.发布相应事件。

11.callRunners  ApplicationRunner  CommandLineRunner

12.发布事件 ApplicationReadyEvent

猜你喜欢

转载自blog.csdn.net/AnY11/article/details/83926597