spring——xml配置文件的读取

spring——xml配置文件的读取

spring xml资源文件读取主要涉及以下几个类关系如图1:
ResourceLoader:定义资源加载器,主要功能是根据给定的资源文件地址返回对应的Resource(Resource 是对资源的封装)
BeanDefinitionReader:主要定义资源文件读取并转换为BeanDefinition的各个功能
EnvironmentCapable:定义获取Environment的方法
DocumentLoader:定义从资源文件加载到转换为Document的功能
AbstractBeanDefinitionReader:对EnvironmentCapable,BeanDefinitionReader类定义的功能进行实现
BeanDefinitionDocumentReader:定义读取Document并注册BeanDefinition功能
BeanDefinitionParserDelegate:定义解析Element的方法
图1
整个xml配置文件读取的大致流程如下:
1.通过继承AbstractBeanDefinitionReader 中的方法,使用ResourLoader将指定路径下的资源文件转换为对应的Resource文件。
2.通过DocumemtLoader对Resource封装的资源文件进行转换,将Resource文件转换为Document文件。
3.通过实现接口BeanDefinitionDocumentReader的DefaultBeanDefinitionDocumentReader类对Document进行解析,并使用BeanDefinitionParserDelegate对Element进行解析。

源码解析:
beanFactoryTest.xml 文件 + 测试类BeanFactoryTest.java代码如下:

<?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-4.0.xsd">
	<bean id="myTestBean" class="com.day1.MyTestBean"/>
</beans>
public class BeanFactoryTest {
   
	@Test
	public void testSimpleLoad(){
   
		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"));
		MyTestBean myTestBean = (MyTestBean) beanFactory.getBean("myTestBean");
		assertEquals("testStr",myTestBean.getTestStr());
	}
}</
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值