Java 我们什么时候会在 Spring 中使用 applicationContext.xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18102191/
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-08-11 22:23:58  来源:igfitidea点击:

When will we use applicationContext.xml in Spring?

javaspringspring-mvc

提问by Praveen P Moolekandathil

Why do we need applicationContext.xmlin Spring?

为什么我们需要applicationContext.xmlSpring?

In what situation would we use it? Do you have an example?

我们会在什么情况下使用它?你有例子吗?

What is the difference between applicationContext.xmland spring-servlet.xml?

applicationContext.xml和 和有spring-servlet.xml什么区别?

How can we compare applicationContext.xmlin Spring with Struts.xmlin Struts for easy understanding?

我们如何比较applicationContext.xmlSpring 和Struts.xmlStruts 以便于理解?

采纳答案by Bitmap

Why do we need applicationContext.xml in Spring?

为什么我们在 Spring 中需要 applicationContext.xml?

In the early days of Spring framework, Application context i.e the various weave and settings necessary to bootstrap, coordinate and control all objects, where done using XML file. Although one can break various settings and dependency injection into several context files, this process has been made easier In Spring 2.5 and later by annotation-driven settings.

在 Spring 框架的早期,应用程序上下文,即引导、协调和控制所有对象所需的各种组织和设置,其中使用 XML 文件完成。尽管可以将各种设置和依赖注入分解为多个上下文文件,但在 Spring 2.5 及更高版本中,通过注释驱动的设置,这一过程变得更加容易。

What is the difference between applicationContext.xml and spring-servlet.xml?

applicationContext.xml 和 spring-servlet.xml 有什么区别?

In a MVC based project, again if you're not using annotation-driven weaving mechanism for your project, all your endpoint servlets can be setup in the spring-servlet.xml. Note that the name of the file is always self chosen.

在基于 MVC 的项目中,如果您的项目没有使用注解驱动的编织机制,那么您的所有端点 servlet 都可以在 spring-servlet.xml 中设置。请注意,文件的名称始终是自选的。

How can we compare applicationContext.xml in Spring with Struts.xml in Struts for easy understanding?

如何比较 Spring 中的 applicationContext.xml 和 Struts 中的 Struts.xml 以便于理解?

They are both similar in terms of what they're trying to achieve. i.e a central location for the application bootstrap settings. Similarly, all settings can be tiered into different files to make it modular.

他们都在努力实现的目标方面相似。即应用程序引导程序设置的中心位置。同样,所有设置都可以分层到不同的文件中以使其模块化。

回答by jax

A Web application can have many servlets running at the same time, therefore:

一个 Web 应用程序可以同时运行多个 servlet,因此:

spring-servlet.xmlwill hold beans only visible to a particular servlet.

spring-servlet.xml将保存仅对特定 servlet 可见的 bean。

You could have many different servlets running

您可以运行许多不同的 servlet

spring-servlet2.xml
spring-servlet3.xml
messaging-servlet.xml 

etc.

等等。

applicationContext.xmlwill hold application wide beans. Therefore all the servlets running will have access to the beans defined in applicationContext.xml. However, this is a one way dependency, your servlets can access you applicationContext.xmlbeans but your applicationContextcannot access any of your servletbeans.

applicationContext.xml将保存应用程序宽 bean。因此,所有运行的 servlet 都可以访问applicationContext.xml. 但是,这是一种单向依赖关系,您的 servlet 可以访问您的applicationContext.xmlbean,但您applicationContext不能访问您的任何servletbean。

回答by sp00m

applicationContextcomes from Spring Framework: it manages the business/DAO beans.

applicationContext来自 Spring Framework:它管理业务/DAO bean。

spring-servletcomes from Spring MVC: it manages the web beans.

spring-servlet来自 Spring MVC:它管理 web bean。