Java Spring Framework中applicationContext.xml和spring-servlet.xml的区别

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

Difference between applicationContext.xml and spring-servlet.xml in Spring Framework

javaspring

提问by user448070

  • Are applicationContext.xmland spring-servlet.xmlrelated anyhow in Spring Framework?
  • Will the properties files declared in applicationContext.xmlbe available to DispatcherServlet?
  • On a related note, why do I need a *-servlet.xmlat all? Why is applicationContext.xmlalone insufficient?
  • applicationContext.xmlspring-servlet.xml在Spring框架无论如何有关系吗?
  • 中声明的属性文件applicationContext.xml是否可用于DispatcherServlet
  • 在相关说明中,为什么我需要一个*-servlet.xml?为什么applicationContext.xml单独不够?

采纳答案by skaffman

Spring lets you define multiple contexts in a parent-child hierarchy.

Spring 允许您在父子层次结构中定义多个上下文。

The applicationContext.xmldefines the beans for the "root webapp context", i.e. the context associated with the webapp.

applicationContext.xml用于“根web应用上下文”,即与该web应用程序相关联的上下文定义了豆。

The spring-servlet.xml(or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xmlfor servlet spring1, spring2-servlet.xmlfor servlet spring2).

spring-servlet.xml(或任何你称呼它)定义了一个servlet的应用程序上下文中的豆。在一个 webapp 中可以有很多这样的,每个 Spring servlet 一个(例如spring1-servlet.xml对于 servlet spring1spring2-servlet.xml对于 servlet spring2)。

Beans in spring-servlet.xmlcan reference beans in applicationContext.xml, but not vice versa.

中的 Beanspring-servlet.xml可以引用 中的 Bean applicationContext.xml,反之则不行。

All Spring MVC controllers must go in the spring-servlet.xmlcontext.

所有 Spring MVC 控制器都必须在spring-servlet.xml上下文中。

In most simple cases, the applicationContext.xmlcontext is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.

在大多数简单的情况下,applicationContext.xml上下文是不必要的。它通常用于包含在 Web 应用程序中的所有 servlet 之间共享的 bean。如果您只有一个 servlet,那么没有什么意义,除非您有特定用途。

回答by Raje

One more point I want to add. In spring-servlet.xmlwe include component scan for Controller package. In following example we include filter annotation for controller package.

我还想补充一点。在spring-servlet.xml我们包括控制器包的组件扫描。在以下示例中,我们为控制器包包含过滤器注释。

<!-- Scans for annotated @Controllers in the classpath -->
<context:component-scan base-package="org.test.web" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

In applicationcontext.xmlwe add filter for remaining package excluding controller.

applicationcontext.xml我们为不包括控制器的剩余包添加过滤器。

<context:component-scan base-package="org.test">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

回答by abishkar bhattarai

Scenario 1

场景一

In client application (application is not web application, e.g may be swing app)

在客户端应用程序中(应用程序不是 Web 应用程序,例如可能是 Swing 应用程序)

private static ApplicationContext context = new  ClassPathXmlApplicationContext("test-client.xml");

context.getBean(name);

No need of web.xml. ApplicationContext as container for getting bean service. No need for web server container. In test-client.xmlthere can be Simple bean with no remoting, bean with remoting.

不需要web.xml。ApplicationContext 作为获取 bean 服务的容器。不需要 Web 服务器容器。在test-client.xml中可以有没有远程处理的简单 bean,有远程处理的 bean。

Conclusion: In Scenario 1 applicationContext and DispatcherServletare not related.

结论:在场景 1 中 applicationContext 和DispatcherServlet不相关。

Scenario 2

场景二

In a server application (application deployed in server e.g Tomcat). Accessed service via remoting from client program (e.g Swing app)

在服务器应用程序中(部署在服务器中的应用程序,例如 Tomcat)。通过远程从客户端程序(例如 Swing 应用程序)访问服务

Define listener in web.xml

web.xml 中定义监听器

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

At server startup ContextLoaderListenerinstantiates beans defined in applicationContext.xml.

在服务器启动时ContextLoaderListener实例化在applicationContext.xml 中定义的 bean 。

Assuming you have defined the following in applicationContext.xml:

假设您在applicationContext.xml 中定义了以下内容:

<import resource="test1.xml" />
<import resource="test2.xml" />
<import resource="test3.xml" />
<import resource="test4.xml" />

The beans are instantiated from all four configuration files test1.xml, test2.xml, test3.xml, test4.xml.

bean 从所有四个配置文件test1.xmltest2.xmltest3.xmltest4.xml中实例化。

Conclusion: In Scenario 2 applicationContext and DispatcherServletare not related.

结论:在场景 2 中 applicationContext 和DispatcherServlet不相关。

Scenario 3

场景3

In a web application with spring MVC.

在带有 spring MVC 的 Web 应用程序中。

In web.xmldefine:

web.xml 中定义:

<servlet>
    <servlet-name>springweb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
</servlet>

<servlet-mapping>
    <servlet-name>springweb</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

When Tomcat starts, beans defined in springweb-servlet.xmlare instantiated. DispatcherServletextends FrameworkServlet. In FrameworkServletbean instantiation takes place for springweb . In our case springwebis FrameworkServlet.

当Tomcat启动时,springweb-servlet.xml中定义的bean被实例化。 DispatcherServlet延伸FrameworkServlet。在FrameworkServletbean 中实例化发生在 springweb 中。在我们的例子中,springweb是 FrameworkServlet。

Conclusion: In Scenario 3 applicationContext and DispatcherServletare not related.

结论:在场景 3 中 applicationContext 和DispatcherServlet不相关。

Scenario 4

场景 4

In web application with spring MVC. springweb-servlet.xmlfor servlet and applicationContext.xmlfor accessing the business service within the server program or for accessing DB service in another server program.

在带有 Spring MVC 的 Web 应用程序中。springweb-servlet.xml用于servlet 和applicationContext.xml用于访问服务器程序中的业务服务或用于访问另一个服务器程序中的DB 服务。

In web.xmlthe following are defined:

web.xml中定义了以下内容:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>springweb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        
</servlet>

<servlet-mapping>
    <servlet-name>springweb</servlet-name>
    <url-pattern>*.action</url-pattern>
</servlet-mapping>

At server startup, ContextLoaderListenerinstantiates beans defined in applicationContext.xml; assuming you have declared herein:

在服务器启动时,ContextLoaderListener实例化在applicationContext.xml 中定义的 bean ;假设您已在此声明:

<import resource="test1.xml" />
<import resource="test2.xml" />
<import resource="test3.xml" />
<import resource="test4.xml" />

The beans are all instantiated from all four test1.xml, test2.xml, test3.xml, test4.xml. After the completion of bean instantiation defined in applicationContext.xml, beans defined in springweb-servlet.xmlare instantiated.

这些 bean 都是从所有四个test1.xmltest2.xmltest3.xmltest4.xml实例化的。在applicationContext.xml 中定义的 bean 实例化完成后,springweb-servlet.xml中定义的 bean被实例化。

So the instantiation order is: the root (application context), then FrameworkServlet.

所以实例化顺序是:根(应用上下文),然后是FrameworkServlet。

Now it should be clear why they are important in which scenario.

现在应该清楚为什么它们在哪种情况下很重要。

回答by Sujata

Application contexts provide a means for resolving text messages, including support for i18n of those messages. Application contexts provide a generic way to load file resources, such as images. Application contexts can publish events to beans that are registered as listeners. Certain operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context. ResourceLoader support: Spring's Resource interface us a flexible generic abstraction for handling low-level resources. An application context itself is a ResourceLoader, Hence provides an application with access to deployment-specific Resource instances. MessageSource support: The application context implements MessageSource, an interface used to obtain localized messages, with the actual implementation being pluggable

应用程序上下文提供了一种解析文本消息的方法,包括对这些消息的 i18n 支持。应用程序上下文提供了一种加载文件资源(例如图像)的通用方法。应用程序上下文可以向注册为侦听器的 bean 发布事件。对容器或容器中 bean 的某些操作,必须以编程方式使用 bean 工厂进行处理,可以在应用程序上下文中以声明方式处理。ResourceLoader 支持:Spring 的 Resource 接口使用灵活的通用抽象来处理低级资源。应用程序上下文本身是一个 ResourceLoader,因此为应用程序提供了对特定部署资源实例的访问权限。MessageSource 支持:应用程序上下文实现 MessageSource,一个用于获取本地化消息的接口,

回答by Krishna

In simple words,

简单来说,

applicationContext.xmldefines the beans that are shared among all the servlets. If your application have more than one servlet, then defining the common resources in the applicationContext.xmlwould make more sense.

applicationContext.xml定义在所有 servlet 之间共享的 bean。如果您的应用程序有多个 servlet,那么在 servlet 中定义公共资源applicationContext.xml会更有意义。

spring-servlet.xmldefines the beans that are related only to that servlet. Here it is the dispatcher servlet. So, your Spring MVC controllers must be defined in this file.

spring-servlet.xml定义仅与该 servlet 相关的 bean。这是调度程序servlet。因此,您的 Spring MVC 控制器必须在此文件中定义。

There is nothing wrong in defining all the beans in the spring-servlet.xmlif you are running only one servlet in your web application.

spring-servlet.xml如果您在 Web 应用程序中只运行一个 servlet,那么定义 中的所有 bean 并没有错。

回答by Rajanikanta Pradhan

In Servlet technology if you want to pass any input to a particular servlet then you need to pass in init param like below code.

在 Servlet 技术中,如果您想将任何输入传递给特定的 servlet,那么您需要像下面的代码一样传入 init 参数。

 <servlet>
    <servlet-name>DBController</servlet-name>
    <servlet-class>com.test.controller.DBController</servlet-class>
    <init-param>
        <param-name>username</param-name>
        <param-value>John</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>DBController</servlet-name>
    <url-pattern>/DBController</url-pattern>
</servlet-mapping>

If you want to pass some in put that is common for all servlets then that time you need to configure context param. Example

如果您想传递一些对所有 servlet 通用的输入,那么此时您需要配置上下文参数。例子

 <context-param>
    <param-name>email</param-name>
    <param-value>[email protected]</param-value>
</context-param>

SO exactly like this when ever we are working with Spring MVC we need to provide some information to Predefined servlet provided by Spring that is DispatcherServlet through init param. So the configuration is as fallows, here we are providing the spring-servlet.xml as init parameter to DispatcherServlet.

因此,当我们使用 Spring MVC 时,就像这样,我们需要通过 init 参数向 Spring 提供的预定义 servlet 即 DispatcherServlet 提供一些信息。所以配置是休耕,这里我们提供 spring-servlet.xml 作为 init 参数给 DispatcherServlet。

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
              http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Spring MVC App</display-name>

    <servlet>
        <servlet-name>SpringController</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringController</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
</web-app>

Again we need some context param. That is applicable for whole application. So we can provide the root context that is applicationcontext.xml The configuration is like this:

我们再次需要一些上下文参数。这适用于整个应用程序。所以我们可以提供根上下文,即 applicationcontext.xml 配置是这样的:

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationcontext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
        <servlet-name>SpringController</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>SpringController</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>