应用程序上下文和Web应用程序上下文有什么区别?
我知道 WebApplicationContext 用于面向Spring MVC架构的应用程序?
WebApplicationContext
我想知道 ApplicationContext 在MVC应用程序中的用途是什么? ApplicationContext 中定义了哪种bean?
ApplicationContext
Web应用程序上下文扩展了Application Context,它旨在与标准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 configs等) . 通常,servlet Spring应用程序中web.xml中定义的所有应用程序上下文都是Web应用程序上下文,这既适用于根webapp上下文,也适用于servlet的app上下文 .
此外,根据Web应用程序上下文功能可能会使您的应用程序更难测试,您可能需要使用MockServletContext类进行测试 .
Difference between servlet and root context Spring允许您构建多级应用程序上下文层次结构,因此如果在当前应用程序上下文中不存在,则将从父上下文中获取所需的bean . 在Web应用程序中,默认情况下有两个层次结构级别:root和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>
(根应用程序上下文由ContextLoaderListener创建,它在web.xml中声明
<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 .
另见:Difference between applicationContext.xml and spring-servlet.xml in Spring Framework
回到Servlet时代,web.xml只能有一个 <context-param> ,因此当服务器加载应用程序时,只有一个上下文对象被创建,并且该上下文中的数据在所有资源之间共享(例如:Servlet和JSP) . 它与上下文中具有数据库驱动程序名称相同,不会更改 . 以类似的方式,当我们在 <contex-param> 中声明contextConfigLocation参数时,Spring会创建一个Application Context对象 .
<context-param>
<contex-param>
<context-param> <param-name>contextConfigLocation</param-name> <param-value>com.myApp.ApplicationContext</param-value> </context-param>
您可以在应用程序中拥有多个Servlet . 例如,您可能希望以一种方式处理/ secure / *请求,而以其他方式处理/非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>
接受的答案是通过,但有官方解释:
WebApplicationContext是普通ApplicationContext的扩展,它具有Web应用程序所需的一些额外功能 . 它与普通的ApplicationContext的不同之处在于它能够解析主题(请参阅使用主题),并且知道它与哪个Servlet相关联(通过指向ServletContext的链接) . WebApplicationContext绑定在ServletContext中,通过在RequestContextUtils类上使用静态方法,如果需要访问它,可以始终查找WebApplicationContext . 引自Spring Web框架参考
顺便说一句,servlet和root上下文是 both webApplicationContext:
ApplicationContext(根应用程序上下文):每个Spring MVC Web应用程序都有一个applicationContext.xml文件,该文件被配置为上下文配置的根 . Spring加载此文件并为整个应用程序创建applicationContext . 此文件由ContextLoaderListener加载,后者在web.xml文件中配置为上下文参数 . 每个Web应用程序只有一个applicationContext .
WebApplicationContext:WebApplicationContext是一个Web感知应用程序上下文,即它具有servlet上下文信息 . 单个Web应用程序可以具有多个WebApplicationContext每个Dispatcher servlet(它是Spring MVC架构的前端控制器)都与WebApplicationContext相关联 . webApplicationContext配置文件* -servlet.xml特定于DispatcherServlet . 由于Web应用程序可以配置多个调度程序servlet来处理多个请求,因此每个Web应用程序可以有多个webApplicationContext文件 .
4 回答
Web应用程序上下文扩展了Application Context,它旨在与标准javax.servlet.ServletContext一起使用,因此它能够与容器通信 .
在WebApplicationContext中实例化的Bean如果实现ServletContextAware接口,也可以使用ServletContext
ServletContext实例有很多可能的事情,例如通过调用getResourceAsStream()方法访问WEB-INF资源(xml configs等) . 通常,servlet Spring应用程序中web.xml中定义的所有应用程序上下文都是Web应用程序上下文,这既适用于根webapp上下文,也适用于servlet的app上下文 .
此外,根据Web应用程序上下文功能可能会使您的应用程序更难测试,您可能需要使用MockServletContext类进行测试 .
Difference between servlet and root context Spring允许您构建多级应用程序上下文层次结构,因此如果在当前应用程序上下文中不存在,则将从父上下文中获取所需的bean . 在Web应用程序中,默认情况下有两个层次结构级别:root和servlet上下文:
.
这允许您作为整个应用程序的单例运行一些服务(Spring Security bean和基本数据库访问服务通常驻留在此处),另一个作为相应servlet中的独立服务运行,以避免bean之间的名称冲突 . 例如,一个servlet上下文将为网页提供服务,另一个将实现无状态Web服务 .
当您使用spring servlet类时,这两个级别的分离是开箱即用的:要配置根应用程序上下文,您应该在web.xml中使用context-param标记
(根应用程序上下文由ContextLoaderListener创建,它在web.xml中声明
)和servlet应用程序上下文的servlet标记
请注意,如果省略init-param,那么spring将在此示例中使用myservlet-servlet.xml .
另见:Difference between applicationContext.xml and spring-servlet.xml in Spring Framework
回到Servlet时代,web.xml只能有一个
<context-param>
,因此当服务器加载应用程序时,只有一个上下文对象被创建,并且该上下文中的数据在所有资源之间共享(例如:Servlet和JSP) . 它与上下文中具有数据库驱动程序名称相同,不会更改 . 以类似的方式,当我们在<contex-param>
中声明contextConfigLocation参数时,Spring会创建一个Application Context对象 .您可以在应用程序中拥有多个Servlet . 例如,您可能希望以一种方式处理/ secure / *请求,而以其他方式处理/非seucre / * . 对于每个Servlet,您都可以拥有一个上下文对象,它是一个WebApplicationContext .
接受的答案是通过,但有官方解释:
顺便说一句,servlet和root上下文是 both webApplicationContext:
ApplicationContext(根应用程序上下文):每个Spring MVC Web应用程序都有一个applicationContext.xml文件,该文件被配置为上下文配置的根 . Spring加载此文件并为整个应用程序创建applicationContext . 此文件由ContextLoaderListener加载,后者在web.xml文件中配置为上下文参数 . 每个Web应用程序只有一个applicationContext .
WebApplicationContext:WebApplicationContext是一个Web感知应用程序上下文,即它具有servlet上下文信息 . 单个Web应用程序可以具有多个WebApplicationContext每个Dispatcher servlet(它是Spring MVC架构的前端控制器)都与WebApplicationContext相关联 . webApplicationContext配置文件* -servlet.xml特定于DispatcherServlet . 由于Web应用程序可以配置多个调度程序servlet来处理多个请求,因此每个Web应用程序可以有多个webApplicationContext文件 .