spring-boot启动源码分析-2-启动初始化
上篇学习到启动流程中的初始化部分,那么这里继续学习spring boot的启动,由于启动部分的非常多,所以这篇只分析一个整体流程,后面再分别在分析学习。
1.相关环境
- 开发工具IDEA
- JDK1.8
- spring-boot 版本 1.5.10
2.继续启动流程分析(run方法)
StopWatch stopWatch = new StopWatch();//统计运行时间
stopWatch.start();
ConfigurableApplicationContext context = null;
FailureAnalyzers analyzers = null;
configureHeadlessProperty();//awt的设置
SpringApplicationRunListeners listeners = getRunListeners(args);//事件广播的实现类
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);//运行参数,项目启动的时候传入的
ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);//创建相关环境
Banner printedBanner = printBanner(environment);//打印banner,就是控制台输出的logo
context = createApplicationContext();//创建上下文,后续讲解
analyzers = new FailureAnalyzers(context);//创建异常分析类,后续讲解
prepareContext(context, environment, listeners, applicationArguments,printedBanner);//一些容器相关的初始化方法,后续讲解
refreshContext(context);//刷新容器,后续讲解
afterRefresh(context, applicationArguments);//容器初始化以后的回调内容,后续讲解
listeners.finished(context, null);//容器启动完成,后续讲解
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
return context;
}
catch (Throwable ex) {
handleRunFailure(context, listeners, analyzers, ex);
throw new IllegalStateException(ex);
}
3.总结
由于这里面的每个方法展开来讲,几乎都能写成一篇文章,所以这个篇幅只做个总结吧,从第一篇中的spring boot 在启动的时候相关运行环境的判断,读取META-INF/spring-factories中的ApplicationContextInitializer和ApplicationListener,到现在的读取SpringApplicationRunListener,设置环境,打印banner,创建上下文,刷新容器等。这就是spring boot 启动的整体流程,后面会对启动中的细节在补充学习,水平有限欢迎指正。