Springboot启动流程解析

初始化SpringApplication

1.main函数 ,执行run方法

2.new 一个SpringApplication对象

3.执行类变量的初始化

4.推断WebApplicationType,此处返回SERVLET

5.从META-INF/factories加载系统引导器BootstrapRegister

6.从META-INF/factories 加载应用初始化器ApplicationContextInitializer

7.从META-INF/factories加载应用监听器ApplicationListener

8.推断主程序类(main)

run SpringApplication对象

1.创建一个StopWatch 计时

2.计时开始

3.创建了一个DefaultBootstrapContext 对象,同时将初始化的开始的状态暴露出去

4.设置为headless模式

5.从META-INF/factories加载RunListeners

6.将springboot的状态暴露出去

7.将main函数传递的args参数封装到DefaultApplicationArguments

8.加载系统环境变量、和SpringBoot的配置文件(yaml等)的信息

9.设置IgnoreBeanInfo为true

10.设置banner 类信息

11.创建一个AnnotationConfigServletWebServerApplicationContext类型的实例,并包含@configuration @Autowired @commonscan 以及事件监听器

12.设置ConfigurableApplicationContext应用上下文的开始状态

13.准备运行上下文环境,并校验需要的bean是否已经注册,同时加载ApplicationConversionService

14.执行下述方法,返回IOC容器

this.postProcessBeanFactory(beanFactory); StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process"); this.invokeBeanFactoryPostProcessors(beanFactory); this.registerBeanPostProcessors(beanFactory); beanPostProcess.end(); this.initMessageSource(); this.initApplicationEventMulticaster(); this.onRefresh(); this.registerListeners(); this.finishBeanFactoryInitialization(beanFactory); this.finishRefresh();

15.this.refreshContext(context);空函数,可以自定义实现,

16.StopWatch.stop

17.打印容器启动过程

18.listeners.started(context);

19.通过SpringApplicationRunListeners将运行状态回调

深度解读:

1.从配置文件加载子类-----SPI机制,减少耦合,增加可配置扩展

2.IOC容器的生成--IOC

3.加载监听机制

4.类加载机制

ps:欢迎共同讨论,指出错误的地方,共同加油~

你可能感兴趣的:(java技术,spring,boot,java,spring)