Spring源码入门——XmlBeanFactory源码学习

XmlBeanFactory虽然在Spring 3.1之后标记为@Deprecated,具体用法改为

XmlBeanFactory factory = new XmlBeanFactory( new ClassPathResource("knights.xml"));
        //替换为
        DefaultListableBeanFactory fac = new DefaultListableBeanFactory();
        BeanDefinitionReader reader = new XmlBeanDefinitionReader(fac);
        reader.loadBeanDefinitions( new ClassPathResource("knights.xml"));

而我们通过XmlBeanFactory的实例化方式可以看到新的写法只是把原来的实例化流程拿出来了而已。而Spring的Jira中写到了这么一段

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);
    }

而Spring的Jira中写到了这么一段话:

  XmlBeanFactory was deprecated because it existed before the ApplicationContext abstraction did, and theApplicationContext hierarchy was specifically designed to accommodate concrete types aligned with different bean definition types (GenericXmlApplicationContext, AnnotationConfigApplicationContext, etc), while the BeanFactory hierarchy represents a more fundamental abstraction not designed for such variability. XmlBeanFactory is the one historical exception to that rule, and the deprecation reflects that fact. Given that GenericXmlApplicationContext provides equal if not greater convenience (given the String- and Resource-based constructors), undeprecating is unlikely at this point.

  先从XmlBeanFactory的继承体系开始(不完整):

 

  概念上来讲,

  •   BeanFactory,定义获取bean及bean的各种属性
  •   BeanDefinitionRegistry,定义对BeanDefinition的各种增删改查操作。
  •   BeanDefinitionReader,定义从资源文件加载为BeanDefinition的流程

  这样我们可以把XmlBeanFactory加载bean的操作分为两个部分

1,读取文件解析为BeanDefinition集合注册给registry

2,读取BeanDefinition并转换为bean实例返回。第一个流程就是BeaDefinitionReader这个接口定义完成的事情,第二步是BeanFactory需要完成的工作。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值