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

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

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

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

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

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


当前回答

基本上我们可以用两种方式创建spring容器对象

使用BeanFactory。 使用ApplicationContext。

都是界面,

使用实现类,我们可以为spring容器创建对象

来看看区别

BeanFactory:

不支持基于注释的依赖注入。 不支持I18N。 默认情况下,它支持惰性加载。 它不允许配置到多个配置文件。

例如:BeanFactory context=new XmlBeanFactory(new Resource(“applicationContext.xml”));

ApplicationContext

支持基于注释的依赖注入。@ autowired, @PreDestroy I18N支持 它默认支持侵略性加载。 它允许配置多个配置文件。

前任: ApplicationContext context=new ClasspathXmlApplicationContext(“applicationContext.xml”);

其他回答

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

@Configuration
public class MyFactory {

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

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

春豆范围文章从贝龙网站

参考Spring Docs中的这个文档:

http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#context-introduction-ctx-vs-beanfactory

5.15.1 bean工厂还是应用上下文?

使用ApplicationContext,除非你有很好的理由不这样做。

Because the ApplicationContext includes all functionality of the BeanFactory, it is generally recommended over the BeanFactory, except for a few situations such as in an Applet where memory consumption might be critical and a few extra kilobytes might make a difference. However, for most typical enterprise applications and systems, the ApplicationContext is what you will want to use. Spring 2.0 and later makes heavy use of the BeanPostProcessor extension point (to effect proxying and so on). If you use only a plain BeanFactory, a fair amount of support such as transactions and AOP will not take effect, at least not without some extra steps on your part. This situation could be confusing because nothing is actually wrong with the configuration.

总而言之:

ApplicationContext包括BeanFactory的所有功能。 一般建议使用前者。

在一些有限的情况下,例如在移动应用程序中,内存消耗可能是关键的。

在这种情况下,使用更轻量级的BeanFactory是合理的。然而,在大多数企业应用程序中,ApplicationContext是您想要使用的。

要了解更多,请参阅我的博客文章:

Spring中的BeanFactory和ApplicationContext的区别——java Spring博客的基础

BeanFactory和ApplicationContext都是从spring IOC容器中获取bean的方法,但仍有一些区别。

BeanFactory是实例化、配置和管理许多bean的实际容器。这些bean通常彼此协作,因此它们之间存在依赖关系。这些依赖关系反映在BeanFactory使用的配置数据中。

BeanFactory和ApplicationContext都是Java接口,而ApplicationContext扩展了BeanFactory。它们都是使用XML配置文件进行配置的。简而言之,BeanFactory提供了基本的控制反转(IoC)和依赖注入(DI)特性,而ApplicationContext提供了高级特性。

BeanFactory由接口“org.springframework.beans”表示。其中BeanFactory有多个实现。

ClassPathResource resource = new ClassPathResource("appConfig.xml");
XmlBeanFactory factory = new XmlBeanFactory(resource);

区别

BeanFactory instantiate bean when you call getBean() method while ApplicationContext instantiate Singleton bean when container is started, It doesn't wait for getBean() to be called. BeanFactory doesn't provide support for internationalization but ApplicationContext provides support for it. Another difference between BeanFactory vs ApplicationContext is ability to publish event to beans that are registered as listener. One of the popular implementation of BeanFactory interface is XMLBeanFactory while one of the popular implementation of ApplicationContext interface is ClassPathXmlApplicationContext. If you are using auto wiring and using BeanFactory than you need to register AutoWiredBeanPostProcessor using API which you can configure in XML if you are using ApplicationContext. In summary BeanFactory is OK for testing and non production use but ApplicationContext is more feature rich container implementation and should be favored over BeanFactory BeanFactory by default its support Lazy loading and ApplicationContext by default support Aggresive loading.

ApplicationContext: 它加载spring配置文件中配置的spring bean,并在CONTAINER启动时管理spring bean的生命周期。它不会等到getBean("springbeanref")被调用。

BeanFactory 它加载spring配置文件中配置的springbean,在调用getBean("springbeanref")时管理springbean的生命周期。因此,当我们在springbean生命周期开始时调用getBean(“springbeanref”)时。