spring第一篇----XmlBeanFactory 小结

跟着《spring源码深度解析》学习spring

做过java开发,我们对以下的代码一定很熟悉,这是最基本的spring使用方式

public static void main(String[] args) throws InterruptedException {
		BeanFactory bf = new XmlBeanFactory(new ClassPathResource("spring-test.xml"));
	}

我们先来看看XmlBeanFactory类:

public class XmlBeanFactory extends DefaultListableBeanFactory {

	private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);

	public XmlBeanFactory(Resource resource) throws BeansException {
		this(resource, null);
	}


	public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException {
		super(parentBeanFactory);
		this.reader.loadBeanDefinitions(resource);
	}

}


XmlBeanFactory继承了DefaultListableBeanFactory类,DefaultListableBeanFactory类是spring注册和加载bean的默认实现;

我们查看上面的源码可以发现其实XmlBeanFactory和DefaultListableBeanFactory的区别只是在于,XmlBeanFactory中使用了Xml读取器XmlBeanDefinitionReader,从而实现了个性化的BeanDefinitionReader读取。


XmlBeanDefinitionReader的主要处理步骤:

1、通过继承AbstractBeanDefinitionReader中的方法,使用ResourceLoader将资源文件路径转换成对应的Resource文件;

2、通过DocumentLoader对Resource文件进行转换,将Resource文件转换成Document文件;

3、通过实现接口BeanDefinitionDocumentReader的DefaultBeanDefinitionDocumentReader类对Document进行解析,并使用BeanDefinitionParserDelegate对Element进行解析

再看一下XmlBeanDefinitionReader相关的几个类:

1、ResourceLoader:定义资源加载器,主要用于根据给定的资源文件地址返回对应的Resource;

2、BeanDefinitionReader:主要用于定义资源文件读取并转换成BeanDefinition的功能;

3、DocumentLoader:定义从资源文件转换为Document文件的功能;

4、AbstractBeanDefinitionReader:对EnvironmentCapable、BeanDefinitionReader类定义的功能进行实现;

5、BeanDefinitionDocumentReader:定义读取Document并注册BeanDefinition的功能;

6、BeanDefinitionParserDelegate:定义解析Element的各种方法。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值