AbstractRefreshableConfigApplicationContext继承于AbstractRefreshableApplicationContext,主要解析配置文件名中占位符。
类作用
AbstractRefreshableConfigApplicationContext主要作用就是根据传入的配置文件名解析占位符(如果有)。主要作用如下:
1、解析配置文件名中的占位符为相应属性值,保存到configLocations。
2、实现接口BeanNameAware–ApplicationContext本身当做一个实例bean,把id设置为beanName
3、实现接口InitializingBean–在afterPropertiesSet,如果容器未刷新,则触发容器的refresh()。
类实现
//参数个数不定
public void setConfigLocations(@Nullable String... locations) {
if (locations != null) {
Assert.noNullElements(locations, "Config locations must not be null");
this.configLocations = new String[locations.length];
//逐一处理文件名
for (int i = 0; i < locations.length; i++) {
this.configLocations[i] = resolvePath(locations[i]).trim();
}
}
else {
this.configLocations = null;
}
}
protected String resolvePath(String path) {
//把path中的${…}占位符替换为从属性文件中解析出的相应属性值。
return getEnvironment().resolveRequiredPlaceholders(path);
}
```
代码可见实现非常简单。