springboot启动流程
2023-01-06 23:35:25 0 举报
AI智能生成
springboot流程
作者其他创作
大纲/内容
初始化springApplication对象
推测web应用类型
1. 如果项目依赖中存在org.springframework.web.reactive.DispatcherHandler,并且不存在
org.springframework.web.servlet.DispatcherServlet,那么应用类型为WebApplicationType.REACTIVE
org.springframework.web.servlet.DispatcherServlet,那么应用类型为WebApplicationType.REACTIVE
2. 如果项目依赖中不存在org.springframework.web.reactive.DispatcherHandler,也不存在
org.springframework.web.servlet.DispatcherServlet,那么应用类型为WebApplicationType.NONE
org.springframework.web.servlet.DispatcherServlet,那么应用类型为WebApplicationType.NONE
3. 否则,应用类型为WebApplicationType.SERVLET
获取BootstrapRegistryInitializer对象
1. 从"META-INF/spring.factories"中读取key为BootstrapRegistryInitializer类型的扩展点,并实例化出对应扩展点
对象
对象
2. BootstrapRegistryInitializer的作用是可以初始化BootstrapRegistry
3. 上面的DefaultBootstrapContext对象就是一个BootstrapRegistry,可以用来注册一些对象,这些对象可以用在从
SpringBoot启动到Spring容器初始化完成的过程中
SpringBoot启动到Spring容器初始化完成的过程中
4、没有Spring容器之前就利用BootstrapRegistry来共享一些对象,有了Spring容器之后就利用Spring容
器来共享一些对象
器来共享一些对象
获取ApplicationContextInitializer对象
1. 从"META-INF/spring.factories"中读取key为ApplicationContextInitializer类型的扩展点,并实例化出对应扩展
点对象
点对象
2、ApplicationContextInitializer是用来初始化Spring容器ApplicationContext对象的,比如可以利用
ApplicationContextInitializer来向Spring容器中添加ApplicationListener
ApplicationContextInitializer来向Spring容器中添加ApplicationListener
获取ApplicationListener对象
1. 从"META-INF/spring.factories"中读取key为ApplicationListener类型的扩展点,并实例化出对应扩展点对象
2. ApplicationListener是Spring中的监听器
推测出Main类(main()方法所在的类)
逻辑是根据当前线程的调用栈来判断main()方法在哪个类,哪个类就是Main类
run(String... args)方法
1. 创建DefaultBootstrapContext对象
2. 利用BootstrapRegistryInitializer初始化DefaultBootstrapContext对象
3. 获取SpringApplicationRunListeners
触发SpringApplicationRunListener的starting()
默认情况下SpringBoot提供了一个EventPublishingRunListener,它实现了
SpringApplicationRunListener接口,默认情况下会利用EventPublishingRunListener发布一个
ApplicationContextInitializedEvent事件,程序员可以通过定义ApplicationListener来消费这个事件
SpringApplicationRunListener接口,默认情况下会利用EventPublishingRunListener发布一个
ApplicationContextInitializedEvent事件,程序员可以通过定义ApplicationListener来消费这个事件
创建Environment对象(表示环境变量)
1. 当前操作系统的环境变量
2. JVM的一些配置信息
3. -D方式所配置的JVM环境变量
触发SpringApplicationRunListener的environmentPrepared()
1、利用EventPublishingRunListener发布一个ApplicationEnvironmentPreparedEvent事
件
件
2、定义ApplicationListener来消费这个事件
创建Spring容器对象(ApplicationContext)
会利用ApplicationContextFactory.DEFAULT,根据应用类型创建对应的Spring容器。
1. 应用类型为SERVLET,则对应AnnotationConfigServletWebServerApplicationContext
2. 应用类型为REACTIVE,则对应AnnotationConfigReactiveWebServerApplicationContext
3. 应用类型为普通类型,则对应AnnotationConfigApplicationContext
10、利用ApplicationContextInitializer初始化Spring容器对象
默认情况下SpringBoot提供了多个ApplicationContextInitializer
1、 将Spring容器赋值给它的applicationContext属性
2. 并且往Spring容器中添加一个ConditionEvaluationReportListener(ConditionEvaluationReportLoggingListener
的内部类),它是一个ApplicationListener
的内部类),它是一个ApplicationListener
3. 并生成一个ConditionEvaluationReport对象赋值给它的report属性
ConditionEvaluationReportListener会负责接收ContextRefreshedEvent事件,也就是Spring容器一
旦启动完毕就会触发ContextRefreshedEvent
旦启动完毕就会触发ContextRefreshedEvent
触发SpringApplicationRunListener的contextPrepared()
1、默认情况下会利用EventPublishingRunListener发布一个ApplicationContextInitializedEvent事件
2、默认情况下暂时没有ApplicationListener消费了这个事件
将启动类作为配置类注册到Spring容器中(load()方法)
将SpringApplication.run(MyApplication.class);中传入进来的类
触发SpringApplicationRunListener的contextLoaded()
默认情况下会利用EventPublishingRunListener发布一个ApplicationPreparedEvent事件
刷新Spring容器
1. AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
2. applicationContext .register(MyApplication.class)
3. applicationContext .refresh()
16、触发SpringApplicationRunListener的started()
1、发布ApplicationStartedEvent事件和AvailabilityChangeEvent事件
2、,AvailabilityChangeEvent事件
表示状态变更状态
表示状态变更状态
3、变更后的状态为LivenessState.CORRECT
LivenessState枚举有两个值
1. CORRECT:表示当前应用正常运行中
2. BROKEN:表示当前应用还在运行,但是内部出现问题,暂时还没发现哪里用到了
17、调用ApplicationRunner和CommandLineRunner
1. 获取Spring容器中的ApplicationRunner类型的Bean
2. 获取Spring容器中的CommandLineRunner类型的Bean
3. 执行它们的run()
18、触发SpringApplicationRunListener的ready()
1、发布ApplicationReadyEvent事件和AvailabilityChangeEvent事件
2、AvailabilityChangeEvent事件
表示状态变更状态
表示状态变更状态
3、变更后的状态为ReadinessState.ACCEPTING_TRAFFIC
ReadinessState枚举有两个值:
1. ACCEPTING_TRAFFIC:表示当前应用准备接收请求
2. REFUSING_TRAFFIC:表示当前应用拒绝接收请求,比如Tomcat关闭时,就会发布AvailabilityChangeEvent事
件,并且状态为REFUSING_TRAFFIC
件,并且状态为REFUSING_TRAFFIC
配置文件解析(优先级由高到低)
1. Default properties (specified by setting SpringApplication.setDefaultProperties).
2. @PropertySource annotations on your @Configuration classes. Please note that such property
sources are not added to the Environment until the application context is being refreshed. This is
too late to configure certain properties such as logging.* and spring.main.* which are read before
refresh begins
sources are not added to the Environment until the application context is being refreshed. This is
too late to configure certain properties such as logging.* and spring.main.* which are read before
refresh begins
3. Config data (such as application.properties files).
4. A RandomValuePropertySource that has properties only in random.*.
5. OS environment variables
6. Java System properties (System.getProperties()).
7. JNDI attributes from java:comp/env. 不管它
8. ServletContext init parameters.
9. ServletConfig init parameters.
10. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment
variable or system property).
variable or system property).
11. Command line arguments.
12. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a
particular slice of your application.
particular slice of your application.
13. @TestPropertySource annotations on your tests.
14. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is
active
active
收藏
0 条评论
下一页