Spring Boot 启动源码解析系列六:执行启动方法一

Posted petewell

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 启动源码解析系列六:执行启动方法一相关的知识,希望对你有一定的参考价值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public ConfigurableApplicationContext (String... args) {

StopWatch stopWatch = new StopWatch();
// 开始执行,记录开始时间
stopWatch.start();

ConfigurableApplicationContext context = null;
FailureAnalyzers analyzers = null;
this.configureHeadlessProperty();

// 获取 SpringApplicationRunListener 实现类,实际只有 EventPublishingRunListener 一个实现类
SpringApplicationRunListeners listeners = this.getRunListeners(args);
// ApplicationStartedEvent 事件监听器执行监听
listeners.starting();

try {
// 实例化应用程序参数持有类
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 实例化容器环境配置信息
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);

// 打印 Banner
Banner printedBanner = this.printBanner(environment);

// 实例化 Spring ApplicationContext
context = this.createApplicationContext();
new FailureAnalyzers(context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
this.refreshContext(context);
this.afterRefresh(context, applicationArguments);

//
listeners.finished(context, (Throwable)null);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}

return context;
} catch (Throwable var9) {
this.handleRunFailure(context, listeners, (FailureAnalyzers)analyzers, var9);
throw new IllegalStateException(var9);
}
}

核心步骤一:启动 SpringApplicationRunListener

1
2
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();

SpringApplicationRunListener 接口:

1
2
3
4
5
6
7
8
9
10
11
public interface SpringApplicationRunListener {
void starting();

void environmentPrepared(ConfigurableEnvironment var1);

void contextPrepared(ConfigurableApplicationContext var1);

void contextLoaded(ConfigurableApplicationContext var1);

void finished(ConfigurableApplicationContext var1, Throwable var2);
}

SpringApplicationRunListener 接口目前只有 EventPublishingRunListener 一个实现类。

1
2
3
4
private SpringApplicationRunListeners getRunListeners(String[] args) {
Class<?>[] types = new Class[]{SpringApplication.class, String[].class};
return new SpringApplicationRunListeners(logger, this.getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));
}

同理也是实例化 SpringApplicationRunListener 接口实现类,最终实际是调用 EventPublishingRunListener 的 starting() 方法:

1
2
3
public void starting() {
this.initialMulticaster.multicastEvent(new ApplicationStartedEvent(this.application, this.args));
}

最终调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
ResolvableType type = eventType != null ? eventType : this.resolveDefaultEventType(event);
Iterator var4 = this.getApplicationListeners(event, type).iterator();

while(var4.hasNext()) {
final ApplicationListener<?> listener = (ApplicationListener)var4.next();
Executor executor = this.getTaskExecutor();
if (executor != null) {
executor.execute(new Runnable() {
public void () {
SimpleApplicationEventMulticaster.this.invokeListener(listener, event);
}
});
} else {
this.invokeListener(listener, event);
}
}

}

SpringApplicationRunListener 会被构建为 AppicationEvent,然后被 ApplicationEventMulticaster 广播,最后转变为 ApplicationListener。

这中间比较复杂,看的也不是特别的深入,后续继续深入分析。

原文:大专栏  Spring Boot 启动源码解析系列六:执行启动方法一


以上是关于Spring Boot 启动源码解析系列六:执行启动方法一的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot Dubbo 应用启停源码分析

spring boot 源码解析 启动流程

Spring Boot自动配置源码解析(基于Spring Boot 2.0.2.RELEASE)

Spring Boot 启动源码解析二

Spring Boot 启动源码解析二

Spring Boot干货系列:启动原理解析