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