BeanFactory 和 ApplicationContext 的区别
这是 Spring Framework 对 ApplicationContext 的解释:
The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.
ApplicationContext 代表 Spring 的 IOC 容器
,负责实例化、配置和组装 bean。
Spring Framework 把 ApplicationContext 称为容器
,其实不是太准确,真正的容器应该是 org.springframework.beans.factory.BeanFactory
,不过 ApplicationContext 是 BeanFactory 的子接口,并提供 AutowireCapableBeanFactory 的引用,这样 ApplicationContext 就拥有了 Bean 读取和装载的能力,因此,一般也笼统的称 ApplicationContext 为容器
。
再回到区别上来,这是 Spring Framework 的描述:
In short, the BeanFactory provides the configuration framework and basic functionality, and the ApplicationContext adds more enterprise-specific functionality.
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
}
可以看到 ApplicationContext 还扩展了其它接口,提供了其它功能:
- 通过 MessageSource 提供 i18n 国际化支持;
- 通过 ResourceLoader 提供资源的加载,比如 class path 或系统资源;
- 通过 ApplicationEventPublisher 对实现了 ApplicationListener 的 bean 进行事件通知;
- 通过 HierarchicalBeanFactory 加载多个上下文;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!