spring BeanFactory 和 ApplicationContext 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21985910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:34:31  来源:igfitidea点击:

Difference between BeanFactory and ApplicationContext?

springioc-container

提问by Vignesh

Both are actually ioc containers. But what is the actual difference between them? Which one is better to use?

两者实际上都是 ioc 容器。但它们之间的实际区别是什么?哪个更好用?

采纳答案by Sudip7

ApplicationContext is derived from BeanFactory to provide added functionality to work in web application.

ApplicationContext 派生自 BeanFactory 以提供在 Web 应用程序中工作的附加功能。

You can instantiate your spring container by just writing

您只需编写即可实例化您的 spring 容器

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

or

或者

ApplicationContext context = new ClassPathXmlApplicationContext{"spring_dao.xml","spring_service.xml};

You can use one or more xml file depending on your project requirement. As I am here using two xml files i.e. one for configuration details for service classes other for dao classes. Here ClassPathXmlApplicationContext is child of ApplicationContext.

您可以根据项目要求使用一个或多个 xml 文件。因为我在这里使用两个 xml 文件,即一个用于服务类的配置详细信息,另一个用于 dao 类。这里 ClassPathXmlApplicationContext 是 ApplicationContext 的孩子。

For better understanding the difference your can check the http://docs.spring.io/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactorysite.

为了更好地理解差异,您可以查看http://docs.spring.io/spring/docs/2.5.x/reference/beans.html#context-introduction-ctx-vs-beanfactory站点。

Choosing between BeanFactory and ApplicationContext is also depends how you want load your beans.

在 BeanFactory 和 ApplicationContext 之间进行选择还取决于您希望如何加载 bean。

ApplicationContext is preferred unless you need to save resources, like on a mobile application.

ApplicationContext 是首选,除非您需要节省资源,例如在移动应用程序上。

回答by Inayathulla

Both BeanFactory and Application are used to manage life cycle of beans, ApplicationContext can do all things that a BeanFactory does along with AOP,Event etc..

BeanFactory 和 Application 都用于管理 bean 的生命周期,ApplicationContext 可以完成 BeanFactory 所做的所有事情以及 AOP、Event 等。

Unless until resources are crucial, go for ApplicationContext.

除非资源至关重要,否则请使用 ApplicationContext。