码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-09-30 12:47:17      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:list   ams   final   exe   ica   inf   eve   refresh   lis   

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 启动源码解析系列六:执行启动方法一

标签:list   ams   final   exe   ica   inf   eve   refresh   lis   

原文地址:https://www.cnblogs.com/petewell/p/11611864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!