应用程序上下文和Web应用程序上下文之间的区别是什么?
我知道WebApplicationContext
用于面向Spring MVC架构的应用程序?
我想知道ApplicationContext
在MVC应用程序中的用途是什么?在ApplicationContext
中定义了哪种bean?
发布于 2017-03-17 00:08:43
回到Servlet时代,web.xml只能有一个<context-param>
,所以当服务器加载应用程序时,只创建一个上下文对象,该上下文中的数据在所有资源(例如:Servlet和JSP)之间共享。这与在上下文中包含数据库驱动程序名称相同,该名称不会更改。以类似的方式,当我们在<contex-param>
中声明contextConfigLocation参数时,Spring会创建一个应用程序上下文对象。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.myApp.ApplicationContext</param-value>
</context-param>
在一个应用程序中可以有多个Servlets。例如,您可能希望以一种方式处理/secure/*请求,而以另一种方式处理/non-seucre/*请求。对于这些Servlets中的每一个,您都可以拥有一个上下文对象,它是一个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>
发布于 2019-02-06 12:40:14
ApplicationContext (根应用程序上下文):每个Spring MVC web应用程序都有一个applicationContext.xml文件,该文件被配置为上下文配置的根。Spring加载此文件并为整个应用程序创建一个applicationContext。此文件由在web.xml文件中配置为上下文参数的ContextLoaderListener加载。并且每个web应用程序只有一个applicationContext。
WebApplicationContext : WebApplicationContext是web感知的应用程序上下文,即它具有servlet上下文信息。一个web应用程序可以有多个WebApplicationContext,并且每个Dispatcher servlet ( Spring MVC架构的前端控制器)都与一个WebApplicationContext相关联。webApplicationContext配置文件*-servlet.xml是特定于DispatcherServlet的。由于web应用程序可以配置多个dispatcher servlet来服务多个请求,因此每个web应用程序可以有多个webApplicationContext文件。
https://stackoverflow.com/questions/11708967
复制相似问题