我是Spring框架的新手,我一直在摆弄它,并把一些样本应用放在一起,目的是评估Spring MVC在即将到来的公司项目中的使用。到目前为止,我真的很喜欢Spring MVC,它看起来很容易使用,并鼓励你编写对单元测试非常友好的类。

作为练习,我正在为我的一个示例/测试项目编写一个主方法。我不清楚的一件事是BeanFactory和ApplicationContext之间的确切区别——哪个适合在哪些条件下使用?

我知道ApplicationContext扩展了BeanFactory,但是如果我只是编写一个简单的主方法,我是否需要ApplicationContext提供的额外功能呢?ApplicationContext究竟提供了什么样的额外功能?

除了回答“我应该在main()方法中使用哪个”之外,关于在这样的场景中应该使用哪个实现,是否有任何标准或指南?我的main()方法是否应该被编写成依赖于bean/应用程序配置的XML格式——这是一个安全的假设吗,还是我将用户锁定到某个特定的东西?

这个答案在web环境中会改变吗——如果我的任何类需要了解Spring,它们更可能需要ApplicationContext吗?

谢谢你的帮助。我知道很多问题都可以在参考手册中找到答案,但如果没有仔细阅读手册,我很难找到这两个接口的清晰分解以及各自的优缺点。


当前回答

Bean工厂的特性矩阵vs来自spring文档的应用程序上下文

beanfactory和ApplicationContext功能的截图

其他回答

spring文档在这方面做得很好:3.8.1。BeanFactory还是ApplicationContext? 他们有一个比较表,我将发布一个片段:

Bean工厂

Bean实例化/布线

应用程序上下文

Bean实例化/布线 自动BeanPostProcessor注册 自动BeanFactoryPostProcessor注册 方便的MessageSource访问(用于i18n) ApplicationEvent出版

因此,如果您需要应用程序上下文端显示的任何点,您应该使用ApplicationContext。

BeanFactory和ApplicationContext的区别如下:

BeanFactory uses lazy initialization but ApplicationContext uses eager initialization. In case of BeanFactory, bean is created when you call getBeans() method, but bean is created upfront in case of ApplicationContext when the ApplicationContext object is created. BeanFactory explicitly provide a resource object using syntax but ApplicationContext creates and manages resource objects on its own. BeanFactory doesnt support internatiolization but ApplicationContext supports internationalization. With BeanFactory annotation based dependency injection is not supported but annotation based dependency injection is supported in ApplicationContext.

使用BeanFactory:

BeanFactory beanfactory = new XMLBeanFactory(new FileSystemResource("spring.xml"));
 Triangle triangle =(Triangle)beanFactory.getBean("triangle");

使用ApplicationContext:

ApplicationContext context = new ClassPathXMLApplicationContext("spring.xml")
Triangle triangle =(Triangle)context.getBean("triangle");

在非web应用程序中使用BeanFactory,因为它只支持单例和原型bean作用域。

而ApplicationContext容器确实支持所有的bean作用域,所以你应该将它用于web应用程序。

我认为值得一提的是,从Spring 3开始,如果您想创建一个工厂,您还可以使用@configuration注释结合适当的@scope

@Configuration
public class MyFactory {

    @Bean
    @Scope("prototype")
    public MyClass create() {
        return new MyClass();
    }
}

使用@ComponentScan注释或xml配置,Spring容器应该可以看到您的工厂

春豆范围文章从贝龙网站

我的六分钱:

BeanFactory and ApplicationContext both are interfaces where ApplicationContext extends BeanFactory. So ApplicationContext is a child interface and BeanFactory is the parent interface. In other words, ApplicationContext has all the features of BeanFactory and it has some additional features that are not present in BeanFactory. BeanFactory loads beans on-demand that is Lazy Loading whereas ApplicationContext loads all beans at startup that is Eager Loading. BeanFactory does not support annotations whereas ApplicationContext supports annotations. ApplicationContext has additional features that are not present in BeanFactory: Internationalization, Event Publishing, AOP features. BeanFactory only supports two scopes — Singleton and Prototype whereas ApplicationContext supports all bean scopes. BeanFactory does not register BeanFactoryPostProcessor and BeanPostProcessor automatically whereas ApplicationContext automatically registers them.