spring-boot启动源码学习-2

本文详细解析了 Spring Boot 的启动流程,包括初始化、创建上下文、刷新容器等关键步骤,并介绍了涉及的相关环境配置及异常处理机制。

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 启动的整体流程,后面会对启动中的细节在补充学习,水平有限欢迎指正

转载于:https://my.oschina.net/nixi0608/blog/1844616

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值