小马哥 Spring-IoC笔记(3)BeanFactory和ApplicationContext的异同

BeanFactory和ApplicationContext

The BeanFactory interface provides an advanced configuration mechanism capable of managing any type of object. ApplicationContext is a sub-interface of BeanFactory. It adds:

  • Easier integration with Spring’s AOP features
  • Message resource handling (for use in internationalization)
  • Event publication
  • Application-layer specific contexts such as the WebApplicationContext for use in web applications.
    In short, the BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality. The ApplicationContext is a complete superset of the BeanFactory .

BeanFactory接口提供了一种高级配置机制,能够管理任何类型的对象。ApplicationContext是BeanFactory的子接口。
ApplicationContext拓展了:

  • 更容易与Spring的AOP特性集成
  • 消息资源处理(用于国际化)
  • 事件发布
  • 应用层特定上下文,如web应用中使用的WebApplicationContext。
    简而言之,BeanFactory提供了配置框架和基本功能,而ApplicationContext添加了更多特定于企业的功能。ApplicationContext是BeanFactory的一个完整超集

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-introduction

BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:META-INF/dependency-injection.xml");
//System.out.println(beanFactory.getBean(BeanFactory.class));
System.out.println("内部注入和外部注入是否是同一个对象:" + (userRepository.getBeanFactory() == beanFactory)); //false

/**
* ApplicationContext 组合了 org.springframework.beans.factory.support.DefaultListableBeanFactory
* DefaultListableBeanFactory 继承了 BeanFactory
* ApplicationContext 又继承了 BeanFactory
* 
* 所以 ApplicationContext != BeanFactory
* 得到 ApplicationContext 时要调 ApplicationContext.getBeanFactory()
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值