应用程序上下文和Web应用程序上下文之间的区别是什么?
我知道WebApplicationContext用于面向Spring MVC架构的应用程序?
我想知道什么是应用程序上下文在MVC应用程序的使用?ApplicationContext中定义了什么类型的bean ?
应用程序上下文和Web应用程序上下文之间的区别是什么?
我知道WebApplicationContext用于面向Spring MVC架构的应用程序?
我想知道什么是应用程序上下文在MVC应用程序的使用?ApplicationContext中定义了什么类型的bean ?
当前回答
公认的答案是通过,但对此有官方解释:
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
顺便说一下,servlet和根上下文都是webApplicationContext:
其他回答
ApplicationContext(根应用程序上下文): 每个Spring MVC web应用程序都有一个applicationContext.xml文件,该文件被配置为上下文配置的根文件。Spring加载这个文件并为整个应用程序创建一个applicationContext。 该文件由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.
回到Servlet时代,web.xml只能有一个<context-param>,所以当服务器加载一个应用程序时,只有一个上下文对象被创建,并且该上下文中的数据在所有资源之间共享(例如:Servlet和jsp)。这与上下文中的数据库驱动程序名称相同,不会更改。类似地,当我们在< Context -param>中声明contextConfigLocation参数时,Spring创建了一个Application Context对象。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.myApp.ApplicationContext</param-value>
</context-param>
在一个应用程序中可以有多个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>
公认的答案是通过,但对此有官方解释:
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
顺便说一下,servlet和根上下文都是webApplicationContext:
Web应用程序上下文,由WebApplicationContext接口指定,是用于Web应用程序的Spring应用程序上下文。它具有常规Spring应用程序上下文的所有属性,假定WebApplicationContext接口扩展了ApplicationContext接口,并为web应用程序添加了检索标准Servlet API ServletContext的方法。
除了标准的Spring bean作用域单例和原型之外,在web应用程序上下文中还有三个额外的作用域可用:
request——将单个bean定义限定为单个HTTP请求的生命周期;也就是说,每个HTTP请求都有自己的bean实例,该实例是在单个bean定义的基础上创建的 session -将单个bean定义作用于HTTP会话的生命周期 application -将单个bean定义限定在ServletContext的生命周期内
Web应用程序上下文扩展了应用程序上下文,它被设计为与标准javax.servlet.ServletContext一起工作,因此它能够与容器通信。
public interface WebApplicationContext extends ApplicationContext {
ServletContext getServletContext();
}
在WebApplicationContext中实例化的bean如果实现了ServletContextAware接口,也可以使用ServletContext
package org.springframework.web.context;
public interface ServletContextAware extends Aware {
void setServletContext(ServletContext servletContext);
}
使用ServletContext实例可以做很多事情,例如通过调用getResourceAsStream()方法访问WEB-INF资源(xml配置等)。 通常,servlet Spring应用程序中Web .xml中定义的所有应用程序上下文都是Web应用程序上下文,这既涉及到根Web应用程序上下文,也涉及到servlet的应用程序上下文。
另外,依赖于web应用程序上下文功能可能会使您的应用程序更难测试,您可能需要使用MockServletContext类进行测试。
servlet和根上下文之间的区别 Spring允许您构建多层应用程序上下文层次结构,因此如果所需的bean不在当前应用程序上下文中,则将从父上下文中获取。在web应用中,默认有两个层次结构级别,根和servlet上下文:
这允许您将一些服务作为整个应用程序的单例运行(Spring Security bean和基本数据库访问服务通常驻留在这里),另一些服务作为相应servlet中的独立服务运行,以避免bean之间的名称冲突。例如,一个servlet上下文将为网页提供服务,而另一个上下文将实现无状态的web服务。
当您使用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>
(根应用上下文是由在web.xml中声明的ContextLoaderListener创建的
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
) 和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>
请注意,如果init-param将被省略,那么spring将在本例中使用myservlet-servlet.xml。
参见:Spring Framework中applicationContext.xml和Spring -servlet.xml的区别