前言
看了 refresh 里各个方法之后,简单了解了各个方法的作用。当然不能只了解就行,想要了解 Spring 容器,细品是必须的。
当然 Spring 这么庞大,不可能一两篇文章就能解释的清楚。
那么就从 prepareRefresh 方法开始。
源码
protected void prepareRefresh() {
// 记录容器开始初始化时间
this.startupDate = System.currentTimeMillis();
// 切换容器为初始化状态
this.closed.set(false);
this.active.set(true);
// 确认Log记录已经开启
if (logger.isDebugEnabled()) {
if (logger.isTraceEnabled()) {
logger.trace("Refreshing " + this);
}
else {
logger.debug("Refreshing " + getDisplayName());
}
}
// 初始化占位符资源,该方法为空
initPropertySources();
// 确认运行环境配置正确
getEnvironment().validateRequiredProperties();
// 创建早期时间监听器容集合,保存早期监听器,也就是在之前已经初始化的监听器。
if (this.earlyApplicationListeners == null) {
this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
}
else {
this.applicationListeners.clear();
this.applicationListeners.addAll(this.earlyApplicationListeners);
}
// 创建早期事件集合
this.earlyApplicationEvents = new LinkedHashSet<>();
}
看下来,prepareRefresh 方法源码不是太复杂。要注意的是这里的initPropertySources()
是个空方法,留给子类扩展,根据方法名字应该是初始化属性配置。Spring 的开发者并不知道 Spring 会运行在哪些平台,不同平台一些符号表示意义是不一样的,虽然 Spring 的开发者也有预期配置,但超出预期怎么办?所以 Spring 的开发者留下这个扩展点,供程序员使用,以满足不同平台需求。
总结
prepareRefresh 方法看下来,并不长,也并不是重点关注学习的方法,因此只需简单了解便可。