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()
*/