版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012099869/article/details/78688521
本文主要内容为: spring 如何通过 XmlBeanFactory 解析并注册 bean.xml 配置文件中的 bean。
bean.xml 配置文件为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="quartzProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations" value="classpath*:properties/quartz/*.properties"/>
<property name="fileEncoding" value="UTF-8"/>
</bean>
</beans>
获取 bean 方式:
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("bean.xml"));
Object obj = bf.getBean("quartzProperties");
System.out.println(obj);
输出为:
一、XmlBeanFactory
二、XmlBeanDefinitionReader
三、DefaultBeanDefinitionDocumentReader
注:此处 preProcessXml(root), postProcessXml(root)
为 模板方法模式,有需要时由用户自定义实现:
- preProcessXml() 为在解析 bean definition 之前处理
- postProcessXml() 为在解析 bean definition 之后处理
此处分为 解析默认元素 和 自定义元素。
1.解析默认标签
分为四种:
- import 标签
- alias 标签
- bean 标签
- beans 标签
未完待续。。。