走进 Spring Boot 之第三步 SpringApplication run 方法
Spring Boot Java 大约 2458 字代码走读
public ConfigurableApplicationContext run(String... args) {
//计算启动时间
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
//异常报告集合
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
//无显示界面,本就是后台服务,无需界面
configureHeadlessProperty();
//获取所有监听器,其实就一个EventPublishingRunListener
//可参见spring.factories文件中的SpringApplicationRunListener配置
SpringApplicationRunListeners listeners = getRunListeners(args);
//启动EventPublishingRunListener
listeners.starting();
try {
//java -jar启动时指定的参数,此处未配置
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
//准备运行环境,加载配置文件解析器,读取配置文件,组成需要的运行环境
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
//打印Banner
Banner printedBanner = printBanner(environment);
//创建上下文,AnnotationConfigServletWebServerApplicationContext
context = createApplicationContext();
//加载spring.factories中的SpringBootExceptionReporter配置项
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
//准备上下文,包括设置运行环境,预处理、初始化上下文
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//刷新上下文,bean类在此方法内初始化
refreshContext(context);
//空实现
afterRefresh(context, applicationArguments);
//停止启动计时,此时已打印出 Started DemoApplication in 633.677 seconds (JVM running for 636.143)
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
//开启所有监听器
listeners.started(context);
//是否有ApplicationRunner/CommandLineRunner,如果有也一起启动,此处没有
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
//运行所有监听器,发布ApplicationReadyEvent事件
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
阅读 2621 · 发布于 2020-04-05
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Kubernetes 本地访问 Service 中的资源阅读 144
-
Java 使用 wait 等待会使 synchronized 升级为重量级锁阅读 1452
-
Windows 子系统 WslRegisterDistribution failed with error: 0xc03a001a阅读 694
-
Oracle 两种连接模式 thin 与 oci 的区别阅读 3209
-
微信小程序开发环境调试接口阅读 3166
-
curl 命令遇到重定向时请求重定向后地址阅读 2628
-
Git 取消某个 commit阅读 3325
-
Linux 常用命令之基础命令阅读 1871
-
Linux zgrep,zcat,zless,zmore 等 zutil 包命令阅读 3396
-
OpenResty 使用 cjson 操作 JSON 数据阅读 5587