prepareRefresh是容器刷新的第一步,过程比较简单,直接看中文注释。
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.
//模版方法,通常在web环境下使用,可以将ServletContext、ServletConfig等记录在运行时环境变量中。
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<>();
}