Spring Boot 的启动流程

Spring Boot

启动流程

构造 SpringBootApplication 对象

首先会创建 SpringBootApplication 对象

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
public static void main(String[] args) {
SpringApplication.run(SpringbootLearnApplication.class, args);
}
// ↓
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class<?>[] { primarySource }, args);
}
// ↓
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return new SpringApplication(primarySources).run(args);
}
// ↓ 构造
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {

this.resourceLoader = resourceLoader;

// primarySources 就是当前 SpringBootApplication 对象
// Assert...

this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));

// 判断是否是 web 项目
this.webApplicationType = WebApplicationType.deduceFromClasspath();

// 从类路径下找到 META-INF/spring.factories 配置的所有 ApplicationContextInitializer,然后保存
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));

//从类路径下找到 META-INF/spring.factories 配置的所有 ApplicationListener
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

this.mainApplicationClass = deduceMainApplicationClass();
}

spring.factories

spring.factories 表示这个组件需要配置的东西

image-20201227170337685

1
2
3
4
5
6
7
8
# PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=\
org.springframework.boot.env.PropertiesPropertySourceLoader,\
org.springframework.boot.env.YamlPropertySourceLoader

# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
# ...

run

Spring Boot 的整个启动流程都封装在 run 方法中,本质上其实就是在 Spring 的基础之上封装,做了大量的扩张

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public ConfigurableApplicationContext run(String... args) {

// 启动开始停止的监听
StopWatch stopWatch = new StopWatch();
stopWatch.start();

// 声明 容器
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();

// 是关于 awt 的
configureHeadlessProperty();

// 通过 SpringFactoriesLoader 查找
// 并加载所有的 SpringApplicationRunListeners
// ( 从 META-INF/spring.factories )
// 通过调用 starting() 方法通知所有的 SpringApplicationRunListeners,告知应用开始启动了
SpringApplicationRunListeners listeners = getRunListeners(args);

// 回调所有的获取 SpringApplicationRunListener.starting() 方法
listeners.starting();

try {

// 封装命令行参数
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);

// 准备环境
// 创建环境完成后
// 回调 SpringApplicationRunListener.environmentPrepared()
// 表示环境准备完成
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);

// 打印 SPRING 那个大图标
Banner printedBanner = printBanner(environment);

// 创建容器,会判断是不是 web,利用反射创建容器
context = createApplicationContext();

// 创建一系列 FailureAnalyzer
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);

// 准备上下文环境
// 将 environment 保存到容器
// 还有 applyInitializers(),回调之前保存的所有的 ApplicationContextInitialize 方法
// 还要回调所有的 SpringApplicationRunListener 的 contextPrepared()
// 还有什么 banner 什么的
prepareContext(context, environment, listeners, applicationArguments, printedBanner);

// prepareContext 完成后回调所有的 SpringApplicationRunListener 的 contextLoaded()
// 刷新容器,容器初始化的过程,扫描、创建、加载所有的组件
// 如果是 web,tomcat 也在这里创建好
// 这里就是传统 Spring 初始化的地方,即 refresh 方法
refreshContext(context);

// 从容器中获取所有的 ApplicationRunner( 先 ) 和 CommandLineRunner( 后 ) 进行回调
afterRefresh(context, applicationArguments);

//完成
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
// throw
}

try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
// throw
}
//返回启动的容器
return context;
}

refreshContext

refresh 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 刷新容器,容器初始化的过程,扫描、创建、加载所有的组件
// 如果是 web,tomcat 也在这里创建好
// 这里就是传统 Spring 初始化的地方,即 refresh 方法
refreshContext(context);

// ↓
private void refreshContext(ConfigurableApplicationContext context) {
// 这儿
refresh(context);

if (this.registerShutdownHook) {
try {
context.registerShutdownHook();
}
catch (AccessControlException ex) {
// Not allowed in some environments.
}
}
}