这一节我们来看一下prepareRefresh方法都做了什么。
首先看一下这个方法的源码:
protected void prepareRefresh() {
//记录开始时间
this.startupDate = System.currentTimeMillis();
//设置状态
this.closed.set(false);
this.active.set(true);
//这里是一个空方法,留给子类进行定制化处理
initPropertySources();
//校验必要的属性
getEnvironment().validateRequiredProperties();
//初始化earlyApplicationListeners,并将所有的Listeners放进去
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
}
else {
//重置earlyApplicationListeners
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
//初始化一个earlyApplicationEvents
this.earlyApplicationEvents = new LinkedHashSet<>();
}
prepareRefresh方法比较简单,除了设置状态和校验一些必要的属性,还初始化了earlyApplicationListeners和earlyApplicationEvents,这两个Set是用来存放项目中定义的监听器和监听事件的。