Spring 源码之核心加载方法(1)

1. prepareRefresh

定位: org.springframework.context.support.AbstractApplicationContext#prepareRefresh
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
// 准备刷新,设置应用开启的时间还有active标志,并且执行一些属性源的初始化工作
protected void prepareRefresh() {
// Switch to active.
// 设置容器启动实现
this.startupDate = System.currentTimeMillis();
// 设置容器的关闭标志位
this.closed.set(false);
// 设置容器的开启标志位
this.active.set(true);

if (logger.isDebugEnabled()) {
if (logger.isTraceEnabled()) {
logger.trace("Refreshing " + this);
}
else {
logger.debug("Refreshing " + getDisplayName());
}
}

// Initialize any placeholder property sources in the context environment.
// 初始化placeholder属性, 默认未实现, 由子类实现
initPropertySources();

// Validate that all properties marked as required are resolvable:
// see ConfigurablePropertyResolver#setRequiredProperties
// 创建并获取环境对象,验证需要的属性文件是否都已经放入环境中
getEnvironment().validateRequiredProperties();

// Store pre-refresh ApplicationListeners...
// 判断刷新前的应用程序监听器集合是否为空,如果为空,则将监听器添加到此集合中
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
}
else {
// Reset local application listeners to pre-refresh state.
// 如果不等于空,则清空集合元素对象
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}

// Allow for the collection of early ApplicationEvents,
// to be published once the multicaster is available...
// 允许应用启动之前的事件,当多播器一旦可用的时候,可用立刻响应发布的事件。
this.earlyApplicationEvents = new LinkedHashSet<>();
}