springboot启动时序概要
2021-06-17 16:13:54 3 举报
这是我个人的总结,非常适合想通过源码来探索SpringBoot特性,或者希望更容易理解源码的人查阅,如果时间有限,大体扫一眼就基本了解SpringBoot的原理,各个组件加载时机,以及扩展点情况,如果边参照本图,边阅读源码的话,相信能让你事半功倍
作者其他创作
大纲/内容
create(contexType)
create
registerSingleton
publish(started)
5
加载META-INF/spring.factories中的扩展实现
loader.load()
该接口职责是1.解析${}格式的文本2.从底层source中获取key的值实现类ConfigurationPropertySourcesPropertyResolver,其支持两种解析方式
registerBeanDefinition
SpringBoot版本:2.5.0 Spring版本:5.3.7
AnnotatedBeanDefinitionReader
replace SystemEnvironmentPropertySource to OriginAwareSystemEnvironmentPropertySource
7
ApplicationContextInitializer
SpringApplication
ApplicationListener
绿色类为Spring的SPI类,即可以遵循其规则,自动加载你自定义的实现类
3
load and starting(bootstrapContext)
primary bean:DemoApplicationscore注解逻辑
RandomValuePropertySourceEnvironmentPostProcessor(处理随机属性)SystemEnvironmentPropertySourceEnvironmentPostProcessor(特性增强)SpringApplicationJsonEnvironmentPostProcessor 处理SPRING_APPLICATION_JSONCloudFoundryVcapEnvironmentPostProcessorConfigDataEnvironmentPostProcessor(处理spring主配置文件+profile)DebugAgentEnvironmentPostProcessorIntegrationPropertiesEnvironmentPostProcessor 处理META-INF/spring.integration.properties
适配PropertySource的解析方式
ApplicationContextInitializedEvent
spring.main.allow-bean-definition-overriding=falsespring.main.banner-mode=offspring.main.lazy-initialization=falsespring.main.sources=com.example.demo2.config...
refresh
ConfigurationWarningsPostProcessor 用于打印错误配置的告警日志,默认会检查ComponentScan注解
注册监听器:ServerPortInfoApplicationContextInitializer ,监听事件:WebServerInitializedEvent
ConfigurableEnvironment
该事件匹配到的监听器:LoggingApplicationListener 日志框架before初始化DelegatingApplicationListener 事件转发机制(无行动)BackgroundPreinitializer 耗时操作预初始化(无行动)
shutdownHook
addBeanFactoryPostProcessor
addApplicationListener
已知的实现有:1.DelegatingApplicationContextInitializer 转发到属性context.initializer.classes指定的处理器2.SharedMetadataReaderFactoryContextInitializer 注册了一个BeanFactoryPostProcessor3.ContextIdApplicationContextInitializer cotextId处理4.ConfigurationWarningsApplicationContextInitializer 注册了一个BeanFactoryPostProcessor5.RSocketPortInfoApplicationContextInitializer 用于注册对RSocketServer初始化事件的监听器6.ServerPortInfoApplicationContextInitializer 用于注册监听器,用于打印启动端口7.ConditionEvaluationReportLoggingListener 注册监听器,监听context刷新和失败事件
listeners.contextPrepared(context)
15
BackgroundPreinitializerDelegatingApplicationListener
AnnotationConfigServletWebServerApplicationContext
load
run
这里相当于把后续事件广播的职责交给了context
14
容器准备
addLast application.properties
9
ConfigurationPropertyName解析方式是为了实现松弛绑定relaxed binding;同一个key可能有以下几种格式acme.my-project.person.first-name(推荐格式)acme.myProject.person.firstNameacme.my_project.person.first_nameACME_MYPROJECT_PERSON_FIRSTNAME
10
ApplicationStartedEvent/AvailabilityChangeEvent
处理逻辑:1.监听器如果实现了ApplicationContextAware,则注入2.所有监听器拷贝至context中
创建并关联
添加懒加载BeanFactoryPostProcessor,如果需要的话LazyInitializationBeanFactoryPostProcessor
BeanDefinitionLoader
1
ApplicationContext
ApplicationPreparedEvent
ApplicationServletEnvironment
初始阶段
该类封装了获取属性时,对属性值的类型转换逻辑比如日期绑定,字符转整型在创建时执行大量转换器的初始化
注册propertySourceRandomValuePropertySource random 处理random开头的属性
注册propertySourceStubPropertySource servletConfigInitParams 预先占坑StubPropertySource servletContextInitParams 预先占坑MutablePropertySources systemProperties java系统属性SystemEnvironmentPropertySource systemEnvironment 环境变量
AnnotationConfigUtils.registerAnnotationConfigProcessors
第二版重点
SpringBoot额外配置
LoggingApplicationListener注册单例:springBootLoggingSystem LoggingSystem注册单例:springBootLogFile LogFile注册单例:springBootLoggerGroups LoggerGroups
add
listeners.contextLoaded(context)
BackgroundPreinitializer 耗时操作异步初始化,多数是以静态方式搜索classpath路径1.ConversionServiceInitializer2.ValidationInitializer3.MessageConverterInitializer4.JacksonInitializer5.CharsetInitializer
2
11
注册单例:org.springframework.boot.context.ContextIdApplicationContextInitializer$ContextId ContextId
ConfigurableConversionService
如果1..env类型为ConfigurableEnvironment2.不存在名为configurationProperties的PropertySource则自动注册一个ConfigurationPropertySourcesPropertySource目的是适配env中所有的PropertySource统一使用ConfigurationPropertyName方式解析属性,如果解析失败,则尝试PropertySourcesPropertyResolver方式解析
contextLoaded
applyInitializers(context)
LoggingApplicationListener 日志初始化解析并设置日志相关的系统属性解析logging.file.name和logging.file.path日志group逻辑日志框架初始化
AvailabilityChangeEvent拥有状态LivenessState.CORRECT
addBefore systemProperties
ConfigurablePropertyResolver
springboot启动时序概要(第一部分)
MutablePropertySources
1.将defaultProperties移到末尾,因为默认值的优先级必须最低2.绑定spring.main属性;即拷贝env中的对应属性到SpringApplication对象中
ClassPathBeanDefinitionScanner
ApplicationReadyEvent/AvailabilityChangeEvent
环境准备
注册propertySourceDefaultPropertiesPropertySource defaultProperties 默认值机制SimpleCommandLinePropertySource commandLineArgs 启动参数
addAfter systemEnvironment
注册propertySourceOriginTrackedMapPropertySource application.properties
EnvironmentPostProcessor
create(contextType)
customizePropertySources
12
ConfigurationClassPostProcessor 扫描并注册所有@Configuration注解类AutowiredAnnotationBeanPostProcessor 处理注入逻辑,例如@AutowiredCommonAnnotationBeanPostProcessor 处理注入逻辑,例如@ResourcePersistenceAnnotationBeanPostProcessor(如果出现)EventListenerMethodProcessorDefaultEventListenerFactory
1.EnvironmentPostProcessorApplicationListener2.LoggingApplicationListener3.BackgroundPreinitializer4.DelegatingApplicationListener
initialize(ConfigurableApplicationContext)
CachingMetadataReaderFactoryPostProcessor 元素数据读取器工厂,首先将子类明确注册为优先级最高的工厂处理器。在执行工厂方法时,注册一个SharedMetadataReaderFactoryBean类型的FactoryBean,并将其关联到ConfigurationClassPostProcessor的依赖中,在ConfigurationClassPostProcessor实例化时,被注入ConcurrentReferenceCachingMetadataReaderFactory,此工厂在读取配置类元数据时发挥作用
load and postProcessEnvironment
contextPrepared(ConfigurableApplicationContext)
setId and registerSingleton
addLast
SpringApplicationRunListener
refresh阶段
13
DemoApplication
这是spring的传统关键节点,后续重点对refresh流程和自动配置实现作图
这是我个人的总结,非常适合想通过源码来探索SpringBoot特性,或者希望更容易理解源码的人查阅如果对你有所帮助的话,请给我点赞哦
6
ApplicationEnvironmentPreparedEvent
配置文件扫描位置:classpath:/classpath:/config/file:./file:./config/file:./config/*/
注册propertySource,前提是特定spring参数或者环境变量存在JsonPropertySource spring.application.json
attach
注册实例:springApplicationArguments ApplicationArgumentsspringBootBanner Banner
该事件类型匹配到以下监听器,图中仅画了重点流程,以执行顺序作为序号1.EnvironmentPostProcessorApplicationListener2.AnsiOutputApplicationListener3.LoggingApplicationListener 日志初始化4.DelegatingApplicationListener 事件转发机制5.BackgroundPreinitializer 耗时操作预初始化6.FileEncodingApplicationListener
8
create bootstrapContext and initialize
callRunners
广播ApplicationStartingEvent
EnvironmentPostProcessorApplicationListener 处理环境的扩展点AnsiOutputApplicationListenerLoggingApplicationListener 日志初始化DelegatingApplicationListener 事件转发机制BackgroundPreinitializer 耗时操作预初始化ParentContextCloserApplicationListenerClearCachesApplicationListenerFileEncodingApplicationListener
addFirst
publish(running)
AvailabilityChangeEvent拥有状态ReadinessState.ACCEPTING_TRAFFIC
4
BootstrapRegistryInitializer
0 条评论
下一页