类作用
AbstractXmlApplicationContext继承于AbstractRefreshableConfigApplicationContext,其唯一作用实现祖先类的loadBeanDefinitions方法:从xml格式的配置中加载Bean定义。
类方法
loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
为beanFactory加载bean定义。
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
#为beanFactoy创建beanDefinition读取器
// Create a new XmlBeanDefinitionReader for the given BeanFactory.
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
// Configure the bean definition reader with this context's
// resource loading environment.
#设定 beanDefinition读取器 环境
beanDefinitionReader.setEnvironment(this.getEnvironment());
#设定 beanDefinition读取器 用到的资源加载器
beanDefinitionReader.setResourceLoader(this);
#设定 beanDefinition读取器 为默认的实体解析器
beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
// Allow a subclass to provide custom initialization of the reader,
// then proceed with actually loading the bean definitions.
#初始化 beanDefinition读取器 :设置xml合法性校验
initBeanDefinitionReader(beanDefinitionReader);
#把配置资源加载到 beanDefinition读取器
loadBeanDefinitions(beanDefinitionReader);
}
概括实现步骤:
1、为beanFactory创建XmlBeanDefinitionReader实例。
2、对XmlBeanDefinitionReaderr实例进行初始化设定。
3、从xml格式的Bean配置中加载Bean定义,xml格式的Bean配置来自configResources和configLocations(xml配置文件位置集合)。
loadBeanDefinitions(beanDefinitionReader)
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
Resource[] configResources = getConfigResources();
if (configResources != null) {
reader.loadBeanDefinitions(configResources);
}
String[] configLocations = getConfigLocations();
if (configLocations != null) {
reader.loadBeanDefinitions(configLocations);
}
}
代码实现就是reader从configResources及configLocations加载类定义。