Sum*_*han 180 spring spring-mvc applicationcontext
应用程序上下文和Web应用程序上下文有什么区别?
我知道WebApplicationContext
用于面向Spring MVC架构的应用程序?
我想知道ApplicationContext
MVC应用程序有什么用?什么样的豆类定义ApplicationContext
?
Bor*_*hov 221
Web应用程序上下文扩展了Application Context,它设计用于标准的javax.servlet.ServletContext,因此它可以与容器通信.
public interface WebApplicationContext extends ApplicationContext {
ServletContext getServletContext();
}
Run Code Online (Sandbox Code Playgroud)
在WebApplicationContext中实例化的Bean如果实现ServletContextAware接口,也可以使用ServletContext
package org.springframework.web.context;
public interface ServletContextAware extends Aware {
void setServletContext(ServletContext servletContext);
}
Run Code Online (Sandbox Code Playgroud)
ServletContext实例有很多可能的事情,例如通过调用getResourceAsStream()方法访问WEB-INF资源(xml configs等).通常,servlet Spring应用程序中web.xml中定义的所有应用程序上下文都是Web应用程序上下文,这既适用于根webapp上下文,也适用于servlet的app上下文.
此外,根据Web应用程序上下文功能可能会使您的应用程序更难测试,您可能需要使用MockServletContext类进行测试.
servlet和根上下文之间的区别
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>
Run Code Online (Sandbox Code Playgroud)
(根应用程序上下文由ContextLoaderListener创建,它在web.xml中声明
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
)和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>
Run Code Online (Sandbox Code Playgroud)
请注意,如果省略init-param,那么spring将在此示例中使用myservlet-servlet.xml.
另请参见:Spring Framework中applicationContext.xml和spring-servlet.xml之间的区别
Ben*_*son 12
回到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>
Run Code Online (Sandbox Code Playgroud)
您可以在应用程序中拥有多个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>
Run Code Online (Sandbox Code Playgroud)
Nic*_*len 10
接受的答案是通过,但有官方解释:
WebApplicationContext是普通ApplicationContext的扩展,它具有Web应用程序所需的一些额外功能.它与普通的ApplicationContext的不同之处在于它能够解析主题(请参阅使用主题),并且知道它与哪个Servlet相关联(通过指向ServletContext的链接).WebApplicationContext绑定在ServletContext中,通过在RequestContextUtils类上使用静态方法,如果需要访问它,可以始终查找WebApplicationContext.
顺便说servlet和根上下文有两个 web应用上下文:
ApplicationContext (Root Application Context) :每个 Spring MVC web 应用程序都有一个 applicationContext.xml 文件,它被配置为上下文配置的根。Spring 加载此文件并为整个应用程序创建一个 applicationContext。该文件由 ContextLoaderListener 加载,该 ContextLoaderListener 在 web.xml 文件中配置为上下文参数。每个 Web 应用程序将只有一个 applicationContext。
WebApplicationContext : WebApplicationContext 是一个网络感知应用程序上下文,即它具有 servlet 上下文信息。单个 Web 应用程序可以有多个 WebApplicationContext,并且每个 Dispatcher servlet(它是 Spring MVC 架构的前端控制器)都与一个 WebApplicationContext 相关联。webApplicationContext 配置文件 *-servlet.xml 特定于 DispatcherServlet。由于一个 Web 应用程序可以有多个调度程序 servlet 配置为服务多个请求,因此每个 Web 应用程序可以有多个 webApplicationContext 文件。
归档时间: |
|
查看次数: |
141301 次 |
最近记录: |