源码分析
先看代码
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.
//protect方法,方法内默认为空,可以方便拓展
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<>();
}
观察代码我们可以看到,主要的方法只是设置开始时间和设置一些标识位,并且对environment的一些必须属性做了一个可解析验证;
小结
准备上下文的过程:设置开始时间和active标识位+验证配置环境的属性可解析