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

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

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

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

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

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


当前回答

为了补充Miguel Ping回答的问题,这里是文档中的另一个部分,也回答了这个问题:

简短的版本:使用ApplicationContext,除非你有很好的理由不这样做。对于那些想要更深入地了解上述建议的“但是为什么”的人,请继续阅读。

(将这篇文章发布给未来可能会读到这个问题的春季新手)

其他回答

我认为最好总是使用ApplicationContext,除非您像其他人所说的那样处于移动环境中。ApplicationContext有更多的功能,你肯定想使用postprocessor,比如RequiredAnnotationBeanPostProcessor, AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor,这将帮助你简化你的Spring配置文件,你可以在你的bean中使用@Required, @PostConstruct, @Resource等注释。

即使您不使用ApplicationContext提供的所有东西,最好还是使用它,然后如果您决定使用一些资源东西,如消息或post处理器,或其他模式来添加事务通知等,您将已经拥有一个ApplicationContext,而不需要更改任何代码。

如果你在写一个独立的应用程序,加载ApplicationContext在主方法中,使用ClassPathXmlApplicationContext,并得到主豆和调用它的运行()(或其他方法)来启动应用程序。如果您正在编写一个web应用程序,使用web . xml中的ContextLoaderListener,创造了从ServletContext ApplicationContext你稍后可以得到它,无论你使用JSP时,JSF, JSTL, struts,挂毯等。

Also, remember you can use multiple Spring configuration files and you can either create the ApplicationContext by listing all the files in the constructor (or listing them in the context-param for the ContextLoaderListener), or you can just load a main config file which has import statements. You can import a Spring configuration file into another Spring configuration file by using <import resource="otherfile.xml" /> which is very useful when you programmatically create the ApplicationContext in the main method and load only one Spring config file.

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

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

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

使用BeanFactory。 使用ApplicationContext。

都是界面,

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

来看看区别

BeanFactory:

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

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

ApplicationContext

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

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

BeanFactory指的是在运行时调用getBean()方法后延迟实例化bean对象的spring容器。

ApplicationContext指的是spring框架,它在部署期间主动实例化bean对象,而不在运行时调用getBean()方法。

我的六分钱:

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.