Spring MVC 中的 ApplicationContext 和 WebApplicationContext 有什么区别?

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

What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?

springspring-mvcapplicationcontext

提问by Sumit Trehan

What is the difference between Application Context and Web Application Context?

应用程序上下文和 Web 应用程序上下文有什么区别?

I am aware that WebApplicationContextis used for Spring MVC architecture oriented applications?

我知道它WebApplicationContext用于面向 Spring MVC 架构的应用程序吗?

I want to know what is the use of ApplicationContextin MVC applications? And what kind of beans are defined in ApplicationContext?

我想知道ApplicationContext在MVC应用程序中有什么用?在 中定义了什么样的 bean ApplicationContext

回答by Boris Treukhov

Web Application context extended Application Context which is designed to work with the standard javax.servlet.ServletContextso it's able to communicate with the container.

Web 应用程序上下文扩展的应用程序上下文,旨在与标准javax.servlet.ServletContext一起使用,因此它能够与容器进行通信。

public interface WebApplicationContext extends ApplicationContext {
    ServletContext getServletContext();
}

Beans, instantiated in WebApplicationContext will also be able to use ServletContext if they implement ServletContextAware interface

在 WebApplicationContext 中实例化的 Bean 如果实现了 ServletContextAware 接口,也将能够使用 ServletContext

package org.springframework.web.context;
public interface ServletContextAware extends Aware { 
     void setServletContext(ServletContext servletContext);
}

There are many things possible to do with the ServletContext instance, for example accessing WEB-INF resources(xml configs and etc.) by calling the getResourceAsStream() method. Typically all application contexts defined in web.xml in a servlet Spring application are Web Application contexts, this goes both to the root webapp context and the servlet's app context.

ServletContext 实例可以做很多事情,例如通过调用 getResourceAsStream() 方法访问 WEB-INF 资源(xml 配置等)。通常,servlet Spring 应用程序中 web.xml 中定义的所有应用程序上下文都是 Web 应用程序上下文,这既指向根 webapp 上下文又指向 servlet 的应用程序上下文。

Also, depending on web application context capabilities may make your application a little harder to test, and you may need to use MockServletContextclass for testing.

此外,根据 web 应用程序上下文功能可能会使您的应用程序更难测试,您可能需要使用MockServletContext类进行测试。

Difference between servlet and root contextSpring allows you to build multilevel application context hierarchies, so the required bean will be fetched from the parent context if it's not present in the current application context. In web apps as default there are two hierarchy levels, root and servlet contexts: Servlet and root context.

servlet 和根上下文之间的区别Spring 允许您构建多级应用程序上下文层次结构,因此,如果当前应用程序上下文中不存在所需 bean,将从父上下文中获取所需 bean。在 Web 应用程序中,默认情况下有两个层次结构级别,root 和 servlet 上下文:Servlet and root context.

This allows you to run some services as the singletons for the entire application (Spring Security beans and basic database access services typically reside here) and another as separated services in the corresponding servlets to avoid name clashes between beans. For example one servlet context will be serving the web pages and another will be implementing a stateless web service.

这允许您将一些服务作为整个应用程序的单例运行(Spring Security bean 和基本数据库访问服务通常驻留在此处),并将另一个作为相应 servlet 中的单独服务运行,以避免 bean 之间的名称冲突。例如,一个 servlet 上下文将为网页提供服务,而另一个将实现无状态 Web 服务。

This two level separation comes out of the box when you use the spring servlet classes: to configure the root application context you should use context-paramtag in your web.xml

当您使用 spring servlet 类时,这两个级别的分离是开箱即用的:要配置根应用程序上下文,您应该在 web.xml 中使用context-param标记

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/root-context.xml
            /WEB-INF/applicationContext-security.xml
    </param-value>
</context-param>

(the root application context is created by ContextLoaderListenerwhich is declared in web.xml

(根应用程序上下文由在 web.xml 中声明的ContextLoaderListener创建

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

) and servlettag for the servlet application contexts

) 和servlet 应用程序上下文的servlet标记

<servlet>
   <servlet-name>myservlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>app-servlet.xml</param-value>
   </init-param>
</servlet>

Please note that if init-param will be omitted, then spring will use myservlet-servlet.xml in this example.

请注意,如果省略 init-param,则在此示例中 spring 将使用 myservlet-servlet.xml。

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

另请参阅:Spring Framework 中 applicationContext.xml 和 spring-servlet.xml 的区别

回答by Ben Tennyson

Going back to Servlet days, web.xml can have only one <context-param>, so only one context object gets created when server loads an application and the data in that context is shared among all resources (Ex: Servlets and JSPs). It is same as having Database driver name in the context, which will not change. In similar way, when we declare contextConfigLocation param in <contex-param>Spring creates one Application Context object.

回到 Servlet 时代,web.xml 只能有一个<context-param>,因此当服务器加载应用程序时只会创建一个上下文对象,并且该上下文中的数据在所有资源(例如:Servlet 和 JSP)之间共享。它与上下文中具有数据库驱动程序名称相同,不会更改。类似地,当我们在<contex-param>Spring 中声明 contextConfigLocation 参数时,会创建一个 Application Context 对象。

 <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>com.myApp.ApplicationContext</param-value>
 </context-param>

You can have multiple Servlets in an application. For example you might want to handle /secure/* requests in one way and /non-seucre/* in other way. For each of these Servlets you can have a context object, which is a WebApplicationContext.

一个应用程序中可以有多个 Servlet。例如,您可能希望以一种方式处理 /secure/* 请求,而以另一种方式处理 /non-seucre/* 请求。对于这些 Servlet 中的每一个,您都可以有一个上下文对象,它是一个 WebApplicationContext。

<servlet>
    <servlet-name>SecureSpringDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>com.myapp.secure.SecureContext</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>SecureSpringDispatcher</servlet-name>
    <url-pattern>/secure/*</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>NonSecureSpringDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextClass</param-name>
        <param-value>com.myapp.non-secure.NonSecureContext</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>NonSecureSpringDispatcher</servlet-name>
    <url-pattern>/non-secure/*</url-patten>
</servlet-mapping>

回答by Nick Allen

The accepted answer is through but there is official explanation on this:

接受的答案是通过,但对此有官方解释:

The WebApplicationContext is an extension of the plain ApplicationContext that has some extra features necessary for web applications. It differs from a normal ApplicationContext in that it is capable of resolving themes (see Using themes), and that it knows which Servlet it is associated with (by having a link to the ServletContext). The WebApplicationContext is bound in the ServletContext, and by using static methods on the RequestContextUtils class you can always look up the WebApplicationContext if you need access to it.

Cited from Spring web framework reference

WebApplicationContext 是普通 ApplicationContext 的扩展,它具有 Web 应用程序所需的一些额外功能。它与普通 ApplicationContext 的不同之处在于它能够解析主题(请参阅使用主题),并且它知道它与哪个 Servlet 相关联(通过具有到 ServletContext 的链接)。WebApplicationContext 绑定在 ServletContext 中,并且通过使用 RequestContextUtils 类上的静态方法,如果您需要访问它,您可以随时查找 WebApplicationContext。

引自Spring Web 框架参考

By the way servlet and root context are bothwebApplicationContext:

顺便说servlet和根上下文有两个web应用上下文:

Typical context hierarchy in Spring Web MVC

Typical context hierarchy in Spring Web MVC

回答by Hetal Rachh

ApplicationContext (Root Application Context) : Every Spring MVC web application has an applicationContext.xml file which is configured as the root of context configuration. Spring loads this file and creates an applicationContext for the entire application. This file is loaded by the ContextLoaderListener which is configured as a context param in web.xml file. And there will be only one applicationContext per web application.

ApplicationContext (Root Application Context) :每个 Spring MVC web 应用程序都有一个 applicationContext.xml 文件,它被配置为上下文配置的根。Spring 加载此文件并为整个应用程序创建一个 applicationContext。该文件由 ContextLoaderListener 加载,该 ContextLoaderListener 在 web.xml 文件中配置为上下文参数。每个 Web 应用程序将只有一个 applicationContext。

WebApplicationContext : WebApplicationContext is a web aware application context i.e. it has servlet context information. A single web application can have multiple WebApplicationContext and each Dispatcher servlet (which is the front controller of Spring MVC architecture) is associated with a WebApplicationContext. The webApplicationContext configuration file *-servlet.xml is specific to a DispatcherServlet. And since a web application can have more than one dispatcher servlet configured to serve multiple requests, there can be more than one webApplicationContext file per web application.

WebApplicationContext : WebApplicationContext 是一个网络感知应用程序上下文,即它具有 servlet 上下文信息。单个 Web 应用程序可以有多个 WebApplicationContext,并且每个 Dispatcher servlet(它是 Spring MVC 架构的前端控制器)都与一个 WebApplicationContext 相关联。webApplicationContext 配置文件 *-servlet.xml 特定于 DispatcherServlet。由于一个 web 应用程序可以有多个调度程序 servlet 配置为服务多个请求,因此每个 web 应用程序可以有多个 webApplicationContext 文件。

回答by DimaSan

Web application context, specified by the WebApplicationContextinterface, is a Spring application context for a web applications. It has all the properties of a regular Spring application context, given that the WebApplicationContextinterface extends the ApplicationContextinterface, and add a method for retrieving the standard Servlet API ServletContextfor the web application.

WebApplicationContext接口指定的Web 应用程序上下文是一个用于 Web 应用程序的 Spring 应用程序上下文。它具有常规 Spring 应用程序上下文的所有属性,因为该WebApplicationContext接口扩展了该ApplicationContext接口,并添加了用于检索ServletContextWeb 应用程序的标准 Servlet API 的方法。

In addition to the standard Spring bean scopes singletonand prototype, there are three additional scopes available in a web application context:

除了标准的 Spring bean 作用域singletonandprototype之外,Web 应用程序上下文中还有三个可用的作用域:

  • request- scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has its own instance of a bean created off the back of a single bean definition
  • session- scopes a single bean definition to the lifecycle of an HTTP Session
  • application- scopes a single bean definition to the lifecycle of a ServletContext
  • request- 将单个 bean 定义范围限定为单个 HTTP 请求的生命周期;也就是说,每个 HTTP 请求都有自己的 bean 实例,该 bean 实例是在单个 bean 定义的后面创建的
  • session- 将单个 bean 定义范围限定为 HTTP 会话的生命周期
  • application- 将单个 bean 定义范围限定为一个 bean 的生命周期 ServletContext