springBoot启动流程

1.启动类里面调用SpringApplication.run(xxx.class,args)方法

2.在SpringApplicaiton的run方法中有两个步骤,首先创建SpringApplicaiton对象,然后再调用run方法。
3.在SpringApplicaiton构造器中调用initialize(sources)方法。
4.initialize方法中,
a.将sources转换成list加到this.sources属性中
b.判断是否为web环境(在类路径下是否可以加载到Servlet和ConfigurableWebApplicationContext)
c.加载Initializers(通过META-INF/spring.factories中键为ApplicationContextInitializer的配置进行加载),dubug发现一共
加载了6个initializer(spring-boot-1.5.10.RELEASE.jar中4个,spring-boot-autoconfigure-1.5.10.RELEASE.jar中2个)
d.加载ApplicationListener(也是通过META-INF/spring.factories),debug发现共加载了10个
e.通过寻找main方法找到启动主类。
5.run方法中
a.StopWatch主要是监控启动过程,统计启动时间,检测应用是否已经启动或者停止。
b.加载SpringApplicationRunListener(也是通过META-INF/spring.factories),默认加载的是EventPublishingRunListener
c.调用RunListener.starting()方法。
d.根据args创建应用参数解析器ApplicationArguments;
e.准备环境变量:获取环境变量environment,将应用参数放入到环境变量持有对象中,监听器监听环境变量对象的变化(listener.environmentPrepared)
f.打印Banner信息(SpringBootBanner)
g.创建SpringBoot的应用上下文(AnnotationConfigEmbeddedWebApplicationContext)
h.prepareContext上下文之前的准备
i.refreshContext刷新上下文
j.afterRefresh(ApplicationRunner,CommandLineRunner接口实现类的启动)
k.返回上下文对象


构造器中

//保存主配置类

//判断当前是否是一个web应用

//从类路径下找到META-INF/spring.facories配置的所有Initializer,保存

//找到所有的listener保存


run方法中
//获取srpingApplicationRunListeners 也是META-INF

//回调所有listener的starting()方法

//封装命令行参数

//创建运行环境,回调所有listener的environmentPrepared()方法

//打印banner

//创建ioc容器

//准备上下文,将运行环境保存其中,根据之前保存的Initializer,调用器initialize(),回调所有listener的contextPrepared()方法

//prepareContext中回调listener的contextLoaded()方法

//初始化ioc容器

//从ioc中获取ApplicationRunner和CommandLineRunner,进行回调

//回调listener的finish()方法

 

几个重要的事件回调机制

配置在META-INFO/spring.factories中的

ApplicationContextInitializer

SpringApplicationRunListener

放在ioc容器中的

ApplicationRunner

CommandLineRunner


 

 

 

posted @ 2018-08-02 15:22  剑小破  阅读(7137)  评论(0编辑  收藏  举报