- 前言
- prepareFresh()方法
- 总结
前文将super方法与setConfigLocations方法都解释完毕,这里正式进入到了refresh方法了。
进入到refresh方法中,首先会有个锁,这个锁在super中已经定义好了,这是为了对refresh与destry操作保证其是原子性的。
本文介绍下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.
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<>();
}
用一个图可以完全显示这个方法到底做了什么,如下图所示。
prepareReflush方法主要设置了容器开启时间、激活状态、关闭状态。然后到了initPropertySources()方法,这个方法进去发现是空的,这里是一个扩展点,我们测试下:
这里重新创建了一个MyClassPathXmlApplicationContext类,需要继承ClassPathXmlApplicationContext类,configLocations也需要设置,然后重写initPropertySources方法。这里一般设置一些属性,通常用
getEnvironment().setRequiredProperties(“”);在springMVC中,这里是MVC的重要扩展点。
getEnvironment().validateRequiredProperties();方法是看是否创建了enviroment,这里在setconfigLocations已经完成了环境创建操作,然后是validateRequiredProperties();这个方法主要做的就是验证需要的属性文件是否都已经放入环境中。
继续往下,初始化了监听器集合以及的监听事件集合。
prepareReflush方法主要做了以下工作:
前戏,做容器刷新前的准备工作 1、设置容器的启动时间 2、设置活跃状态为true 3、设置关闭状态为false 4、对initPropertySources扩展点 5、获取Environment对象,并验证属性文件是否都已经放入环境中 6、准备监听器和事件的集合对象,默认为空的集合



