SpringBoot 启动过程和自动装配
SpringBoot启动过程
- SpringBoot启动注解了@SpringBootApplication 的 xxxApplication的main方法中,SpringApplication.run(DemoApplication.class, args)这个是启动的开始位置。主要分为两个步骤:
- 1、SpringApplication初始化META_INF/spring.factories中11个ApplicationListener类型的Listener对象和 7个ApplicationContextInitializer类型的Initializer对象。
- 2、SpringApplication对象run()方法完成SpringBoot的启动过程。SpringBoot的启动过程有7中状态:ApplicationStartingEvent、ApplicationEnvironmentPreparedEvent、ApplicationContextInitializedEvent、ApplicationPreparedEvent、ApplicationStartedEvent、ApplicationReadyEvent和ApplicationFailedEvent。通过观察者模式,当有状态事件发生时,触发11个ApplicationListener中能够处理的Listener进行事件处理。SpringBoot的启动过程主要分为:1)配置环境变量(系统环境变量、配置文件参数);2)打印启动的banner信息;3)创建webApplicationType类型的Context(SERVLET、REACTIVE、 NONE);4)调用applyInitializers()进行7个ApplicationContextInitializer对象进行初始化添加3个Processor(CachingMetadataReaderFactoryPostProcessor、ConfigurationWarningsPostProcessor、PropertySourceOrderingPostProcessor和Listener添加,以及其他初始化操作,打印starting启动日志,beanFactory load SpringBootApplication的SpringBoot启动类,向beanFactory 注册到beanDefinitionMap中;5)调用spring AbstractApplicationContext refresh进行类的加载
context进行简单的属性初始化准备下环境;6)打印服务启动的时间的日志。具体流程见图:
- 3启动流程的关键源码:
//在SpringApplication构造函数中初始化
//META_INF/spring.factories中11个ApplicationListener类型的Listenner对象和 7个ApplicationContextInitializer类型的Initializer对象
//# Application Listeners spring-boot
//org.springframework.context.ApplicationListener=\
//org.springframework.boot.ClearCachesApplicationListener,\
//org.springframework.boot.builder.ParentContextCloserApplicationListener,\
//org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
//org.springframework.boot.context.FileEncodingApplicationListener,\
//org.springframework.boot.context.config.AnsiOutputApplicationListener,\
//org.springframework.boot.context.config.ConfigFileApplicationListener,\
//org.springframework.boot.context.config.DelegatingApplicationListener,\
//org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\
//org.springframework.boot.context.logging.LoggingApplicationListener,\
//org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
//# Application Listeners spring-boot-autoconfigure
//org.springframework.context.ApplicationListener=\
//org.springframework.boot.autoconfigure.BackgroundPreinitializer
//# Application Context Initializers
//org.springframework.context.ApplicationContextInitializer=\
//org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
//org.springframework.boot.context.ContextIdApplicationContextInitializer,\
//org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
//org.springframework.boot.rsocket.context.RSocketPortInfoApplicationContextInitializer,\
//org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer
//# Initializers
//org.springframework.context.ApplicationContextInitializer=\
//org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\
//org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources,<