ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别
来源:互联网 发布:linux vi编辑怎么换行 编辑:程序博客网 时间:2024/05/19 23:15
ContextLoaderListener和DispatcherServlet都会在Web容器启动的时候加载一下bean配置. 区别在于:
- DispatcherServlet一般会加载MVC相关的bean配置管理(如: ViewResolver, Controller, MultipartResolver, ExceptionHandler, etc.)
- ContextLoaderListener一般会加载整个Spring容器相关的bean配置管理(如: Log, Service, Dao, PropertiesLoader, etc.)
DispatcherServlet默认使用WebApplicationContext作为上下文.
DispatcherServlet也可以配置自己的初始化参数,覆盖默认配置:
参数 描述
contextClass
实现WebApplicationContext接口的类,当前的servlet用它来创建上下文。如果这个参数没有指定, 默认使用XmlWebApplicationContext。
contextConfigLocation
传给上下文实例(由contextClass指定)的字符串,用来指定上下文的位置。这个字符串可以被分成多个字符串(使用逗号作为分隔符) 来支持多个上下文(在多上下文的情况下,如果同一个bean被定义两次,后面一个优先)。
默认为/WEB-INF/[server-name]-servlet.xml
namespace
WebApplicationContext命名空间。默认值是[server-name]-servlet。
如下:
<servlet> <servlet-name>demo</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet-config.xml</param-value> </init-param></servlet><servlet-mapping> <servlet-name>demo</servlet-name> <url-pattern>/</url-pattern></servlet-mapping>
值得注意的是, DispatcherServlet的上下文仅仅是Spring MVC的上下文, 而ContextLoaderListener的上下文则对整个Spring都有效. 一般Spring web项目中同时会使用这两种上下文.
上下文创建完后会放在ServletContext对象中, 其中:
1) ContextLoaderListener加载的上下文放在ServletContext的key为WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE属性中;
2) DispatcherServlet加载的上下文在每次请求时会放一份在request对象的key为WEB_APPLICATION_CONTEXT_ATTRIBUTE属性中.
因而两者的获取方式也不一样, 前者可以通过:
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)或
WebApplicationContextUtils.getWebApplicationContext(servletContext)或
WebApplicationContextUtils.getWebApplicationContext(servletContext,attrname)方法来获取对应的applicationContext,
而后者则通过:
RequestContextUtils.getWebApplicationContext(request)或
WebApplicationContextUtils.getWebApplicationContext(servletContext,attrname)方法来获取对应的applicationContext.
(注: 对于ContextLoaderListener加载的上下文, attrname即上面提到的WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
而对于DispatcherServlet中的上下文则为FrameworkServlet.class.getName() + ".CONTEXT." + getServletName())
通过上下文所在的属性可以看出,如果通过WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)来试图获取DispatcherServlet加载的applicationContext时, 就会抛出"No WebApplicationContext found: no ContextLoaderListener registered?"的异常.
注:本文部分转载自网络, 其中大多数参考了ContextLoaderListener和Spring MVC中的DispatcherServlet学习
- ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别
- ContextLoaderListener和Spring MVC中的DispatcherServlet学习
- ContextLoaderListener和Spring MVC中的DispatcherServlet学习
- ContextLoaderListener和Spring MVC中的DispatcherServlet学习
- ContextLoaderListener和Spring MVC中的DispatcherServlet学习
- Spring中DispatcherServlet和ContextLoaderListener的区别
- Spring中ContextLoaderListener和DispatcherServlet的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
- spring中ContextLoaderListener和DispatcherServlet区别 ApplicationContext
- spring中ContextLoaderListener和DispatcherServlet区别
- ContextLoaderListener与DispatcherServlet所加载的区别
- Spring中ContextLoaderListener和DispatcherServlet的差异
- contextLoaderListener和dispatcherServlet初始化上下文的区别
- nyoj 860 又见01背包(01 背包)
- 【1】【状压DP】POJ3311 Hie with the Pie
- 学习用例:js中回文的处理
- Hadoop:HDFS文件存取机制
- ld: cannot find -lc
- ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别
- Pro JPA2读书笔记系列(十一)-第十章(高级对象-关系映射)
- 获取字符中Url地址
- SVN建立分支和合并代码
- 网络请求xml PULL解析
- mysql 字符集(CHARACTER SET)和校对集(COLLATE)
- 架构:Android的MVC
- HDU 4588 Count The Carries(数学)
- udev使用方法(附实例)