BeanFactory 与 ApplicationContext [英] BeanFactory vs ApplicationContext
问题描述
我对 Spring Framework 还很陌生,我一直在尝试使用它并将一些示例应用程序放在一起,以评估 Spring MVC 以用于即将到来的公司项目.到目前为止,我真的很喜欢我在 Spring MVC 中看到的东西,它看起来非常易于使用,并鼓励您编写对单元测试非常友好的类.
I'm pretty new to the Spring Framework, I've been playing around with it and putting a few samples apps together for the purposes of evaluating Spring MVC for use in an upcoming company project. So far I really like what I see in Spring MVC, seems very easy to use and encourages you to write classes that are very unit test-friendly.
作为练习,我正在为我的一个示例/测试项目编写一个主要方法.我不清楚的一件事是 BeanFactory
和 ApplicationContext
之间的确切区别 - 哪个适合在哪些条件下使用?
Just as an exercise, I'm writing a main method for one of my sample/test projects. One thing I'm unclear about is the exact differences between BeanFactory
and ApplicationContext
- which is appropriate to use in which conditions?
我知道 ApplicationContext
扩展了 BeanFactory
,但如果我只是编写一个简单的 main 方法,我是否需要 ApplicationContext
的额外功能> 提供?ApplicationContext
到底提供了哪些额外的功能?
I understand that ApplicationContext
extends BeanFactory
, but if I'm just writing a simple main method, do I need the extra functionality that ApplicationContext
provides? And just exactly what kind of extra functionality does ApplicationContext
provide?
除了回答我应该在 main() 方法中使用哪个"之外,是否有任何标准或指南来说明在这种情况下我应该使用哪种实现?我的 main() 方法是否应该被编写为依赖于 XML 格式的 bean/应用程序配置 - 这是一个安全的假设,还是我将用户锁定在特定的东西上?
In addition to answering "which should I use in a main() method", are there any standards or guidelines as far as which implementation I should use in such a scenario? Should my main() method be written to depend on the bean/application configuration to be in XML format - is that a safe assumption, or am I locking the user into something specific?
这个答案在网络环境中是否会发生变化——如果我的任何类需要了解 Spring,他们是否更有可能需要 ApplicationContext
?
And does this answer change in a web environment - if any of my classes needed to be aware of Spring, are they more likely to need ApplicationContext
?
感谢您的帮助.我知道很多这些问题可能在参考手册中得到了回答,但我很难找到这两个界面的明确分类以及每个界面的优缺点,而不是用细齿梳通读手册.
Thanks for any help. I know a lot of these questions are probably answered in the reference manual, but I'm having a hard time finding a clear breakdown of these two interfaces and the pros/cons of each without reading thru the manual with a fine-tooth comb.
推荐答案
spring 文档在这方面非常出色:3.8.1.BeanFactory 还是 ApplicationContext?.他们有一个比较表,我会发布一个片段:
The spring docs are great on this: 3.8.1. BeanFactory or ApplicationContext?. They have a table with a comparison, I'll post a snippet:
豆工厂
- Bean 实例化/接线
应用上下文
- Bean 实例化/接线
- 自动 BeanPostProcessor 注册
- 自动 BeanFactoryPostProcessor 注册
- 方便的 MessageSource 访问(适用于 i18n)
- ApplicationEvent 发布
因此,如果您需要在应用程序上下文方面提供任何要点,则应使用 ApplicationContext.
So if you need any of the points presented on the Application Context side, you should use ApplicationContext.
这篇关于BeanFactory 与 ApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!