Spring MVC中的ApplicationContext和WebApplicationContext有什么区别? [英] What is the difference between ApplicationContext and WebApplicationContext in Spring MVC?

查看:607
本文介绍了Spring MVC中的ApplicationContext和WebApplicationContext有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Application Context和Web Application Context有什么区别?

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

我知道WebApplicationContext用于面向Spring MVC体系结构的应用程序吗?

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

我想知道ApplicationContext在MVC应用程序中的用途是什么?在ApplicationContext中定义了哪种bean?

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

推荐答案

Web应用程序上下文扩展了应用程序上下文,旨在与标准

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

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

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

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

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

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

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.

此外,取决于Web应用程序上下文的功能,可能会使您的应用程序难以测试,并且您可能需要使用

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

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

Difference between servlet and root context Spring 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: .

这允许您将某些服务作为整个应用程序的单例运行(Spring Security Bean和基本数据库访问服务通常位于此处),而另一项则作为相应服务中的单独服务运行,以避免Bean之间发生名称冲突.例如,一个Servlet上下文将为网页提供服务,而另一个将实现无状态Web服务.

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 servlet类时,这两个级别的分离是开箱即用的:要配置根应用程序上下文,应在web.xml中使用 context-param 标记

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-param tag in your web.xml

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

(根应用程序上下文是由 ContextLoaderListener

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

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

) 和 servlet 标签用于servlet应用程序上下文

) and servlet tag for the servlet application contexts

<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>

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

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

另请参阅: applicationContext.xml与spring- Spring Framework中的servlet.xml

这篇关于Spring MVC中的ApplicationContext和WebApplicationContext有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆