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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| // org.springframework.boot.SpringApplication#run
public ConfigurableApplicationContext run(String... args) {
long startTime = System.nanoTime();
// 创建默认 Boot 启动上下文`org.springframework.boot.DefaultBootstrapContext`;
// 并调用扩展点回调方法`org.springframework.boot.BootstrapRegistryInitializer#initialize`。
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
ConfigurableApplicationContext context = null;
// 配置 headless,略。
configureHeadlessProperty();
// 获取自动配置的`SpringApplicationRunListener.class`实例。
SpringApplicationRunListeners listeners = getRunListeners(args);
// 发布`ApplicationStartingEvent`事件,有若干个监听器生效。
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
// 封装 SpringApplication 传递的参数。
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 构建`Environment`,完成了配置文件加载、profile 启用等工作。
// 发布`ApplicationEnvironmentPreparedEvent`事件。
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
// 配置`spring.beaninfo.ignore`属性,略。
configureIgnoreBeanInfo(environment);
// 打印 Banner(横幅),略。
Banner printedBanner = printBanner(environment);
// 创建`ConfigurableApplicationContext`,默认创建注解驱动的`AnnotationConfigServletWebServerApplicationContext`。
context = createApplicationContext();
// 为 context 配置`ApplicationStartup`。
context.setApplicationStartup(this.applicationStartup);
// 配置`ConfigurableApplicationContext`。
// 先后发布`ApplicationContextInitializedEvent`和`ApplicationPreparedEvent`两个事件。
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
// 刷新 Context,注册和预创建 bean。
refreshContext(context);
// 空逻辑,略。
afterRefresh(context, applicationArguments);
Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);
}
// 发布`ApplicationStartedEvent`事件。
listeners.started(context, timeTakenToStartup);
// 调用回调方法,支持`ApplicationRunner`和`CommandLineRunner`两类。
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
// 异常处理,发布`ApplicationFailedEvent`事件。
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
// 发布`ApplicationReadyEvent`事件。
listeners.ready(context, timeTakenToReady);
}
catch (Throwable ex) {
// 异常处理,发布`ApplicationFailedEvent`事件。
handleRunFailure(context, ex, null);
throw new IllegalStateException(ex);
}
return context;
}
|