首页 > 其他 > 详细

项目的初始化启动**

时间:2020-06-18 16:04:12      阅读:68      评论:0      收藏:0      [点我收藏+]
分析完(newSpringApplication(primarySources)).run(args)源码前一部分SpringApplication实例对象的初始化创建后,查看run(args)方法执行的项目初始化启动过程,核心代码具体如下:

```java
publicConfigurableApplicationContextrun(String... args) {
StopWatchstopWatch= new StopWatch();
stopWatch.start();
ConfigurableApplicationContextcontext = null;
Collection<SpringBootExceptionReporter>exceptionReporters= new ArrayList();
this.configureHeadlessProperty();
// 第一步:获取并启动监听器
SpringApplicationRunListenerslisteners = this.getRunListeners(args);
listeners.starting();
CollectionexceptionReporters;
try{
ApplicationArgumentsapplicationArguments=
new DefaultApplicationArguments(args);
// 第二步:根据SpringApplicationRunListeners以及参数来准备环境
ConfigurableEnvironmentenvironment =
this.prepareEnvironment(listeners,applicationArguments);
this.configureIgnoreBeanInfo(environment);
// 准备Banner打印器 - 就是启动Spring Boot的时候打印在console上的ASCII艺术字体
BannerprintedBanner = this.printBanner(environment);

//第三步:创建Spring容器
context= this.createApplicationContext();
exceptionReporters=
this.getSpringFactoriesInstances(SpringBootExceptionReporter.class,
newClass[]{ConfigurableApplicationContext.class}, new Object[]{context});

// 第四步:Spring容器前置处理
this.prepareContext(context, environment,listeners,
applicationArguments, printedBanner);

// 第五步:刷新容器
this.refreshContext(context);

// 第六步:Spring容器后置处理
this.afterRefresh(context,applicationArguments);
stopWatch.stop();
if(this.logStartupInfo){
(newStartupInfoLogger(this.mainApplicationClass))
.logStarted(this.getApplicationLog(),stopWatch);
}
// 第七步:发出结束执行的事件
listeners.started(context);

// 返回容器
this.callRunners(context,applicationArguments);
}catch (Throwable var10) {
this.handleRunFailure(context,var10, exceptionReporters, listeners);
thrownew IllegalStateException(var10);
}
try{
listeners.running(context);
returncontext;
}catch (Throwable var9) {
this.handleRunFailure(context,var9, exceptionReporters,
(SpringApplicationRunListeners)null);
thrownew IllegalStateException(var9);
}
}

```
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
这些内容,是从拉勾教育的《Java工程师高薪训练营》里学到的,课程内容非常全面,还有拉勾的内推大厂服务,推荐你也看看。

项目的初始化启动**

原文:https://www.cnblogs.com/lagoujiaoyu/p/13157967.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!