public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {
//默认配置文件地址
public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
//为名称空间构建配置位置的默认前缀
public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
*//** Default suffix for building a config location for a namespace *//*
//为名称空间构建配置位置的默认后缀
public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
//解析applicationContext.xml
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
//为给定的BeanFactory创建一个新的XmlBeanDefinitionReader
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
//用这个上下文配置bean定义读取器
// resource loading environment.
//资源加载环境
beanDefinitionReader.setEnvironment(getEnvironment());
beanDefinitionReader.setResourceLoader(this);
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
//允许子类提供读取器的自定义初始化
initBeanDefinitionReader(beanDefinitionReader);
//实际加载bean定义
loadBeanDefinitions(beanDefinitionReader);
}
//初始化子类读取器构造函数
protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
}
//实际意义得加载定义bean
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
reader.loadBeanDefinitions(configLocation);
}*
}
}/
//获取默认配置文件地址
@Override
protected String[] getDefaultConfigLocations() {
if (getNamespace() != null) {
return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};
}
else {
return new String[] {DEFAULT_CONFIG_LOCATION};
}
}