5.1Spring源码解析——ApplicationContext介绍

 前面介绍了BeanFactory接口以及它的默认实现类XmlBeanFactory为例解析和加载创建bean的过程进行分析。但是我们用的更多的时ApplicationContext接口来加载Bean,这两个接口作用都是用来加载Bean的,但是相比之下,Application-Context提供了更多扩展功能,简单一点说:ApplicationContext包含BeanFactory的所有功能。通常建议比BeanFactory优先除非在一些限制的场合,比如字节长度对内存有很大的影响时。
比较使用两个不同的类去加载配置文件在写法上的不同
 使用BeanFactory方式加载XML

BeanFactory bf=new XmlBeanFactory(new ClassPathResource("beanFactoryTest.xml"))

使用ApplicationContext方式加载XML

ApplicationContext ctx = new ClassPathXmlApplicationContext("app.xml");

同样的还是以ClassPathXmlApplicationContext作为切入点。
ClassPathXmlApplicationContext.java

//其中configLocations为数组形式的xml形式的配置文件路径
    public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {
        this(configLocations, true, null);
    }

    public ClassPathXmlApplicationContext(
            String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
            throws BeansException {

        super(parent);
      //todo 设置配置文件路径
        setConfigLocations(configLocations);
        if (refresh) {
          //加载刷新配置,可能是xml文件也可能是其他属性文件
            refresh();
        }
    }

关于refresh方法的详解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值