Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别
2016-03-14 14:30
381 查看
点击查看原文地址
这里做个说明
DispatcherServlet所加载的applicationContext可以认为是mvc私有的context。
通过web.xml配置ContextLoaderListener监听所加载的applicationContext,是共有的context。也就是说,DispatcherServlet的context是继承ContextLoaderListener的context。例如 DispatcherServlet 的上下文环境中有一个类Son,在ContextLoaderListener的上下文环境中是访问不到的。但是ContextLoaderListener的上下文环境中有一类Father,在DispatcherServlet 的上下文环境中是可以访问到的。
spring通过在web.xml 中配置ContextLoaderListener 来加载context配置文件,在 DispatcherServlet中也可以来加载spring context配置文件,那么这两个有什么区别呢。 ContextLoaderListener中加载的context成功后,spring 将 applicationContext存放在ServletContext中key值 为"org.springframework.web.context.WebApplicationContext.ROOT"的attribute中。 (servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context));可以通过 WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)或 WebApplicationContextUtils.getWebApplicationContext(servletContext)方法来获取对应的applicationContext。 DispatcherServlet加载的context成功后,如果 publishContext属性的值设置为true的话(缺省为true) 会将applicationContext存放在 org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName)的attribute中。 例如 web.xml中配置如下 Xml代码 <servlet> <servlet-name>mvcServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring/config/applicationContextMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 则对应的applicationContext的attribute key值为org.springframework.web.servlet.FrameworkServlet.CONTEXT.mvcServlet。 在每次request请求时,DispatcherServlet会将此applicationContext存放在request中attribute值为 org.springframework.web.servlet.DispatcherServlet.CONTEXT中 (request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE,getWebApplicationContext());)。 可以通过 RequestContextUtils.getWebApplicationContext 或 WebApplicationContextUtils.getWebApplicationContext(servletContext,attrname)方法 来获取对应的applicationContext。 从上面的分析可以看出,DispatcherServlet所加载的applicationContext可以认为是mvc私有的context,由于保存在servletContext中的key值与通过 ContextLoaderListener加载进来的applicationContext使用的key值不相同,因此如果只使用DispatcherServlet加载context的话,如果程序中有地方使用 WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) 来试图获取applicationContext时,就会抛出"No WebApplicationContext found: no ContextLoaderListener registered?"的exception。
这里做个说明
DispatcherServlet所加载的applicationContext可以认为是mvc私有的context。
通过web.xml配置ContextLoaderListener监听所加载的applicationContext,是共有的context。也就是说,DispatcherServlet的context是继承ContextLoaderListener的context。例如 DispatcherServlet 的上下文环境中有一个类Son,在ContextLoaderListener的上下文环境中是访问不到的。但是ContextLoaderListener的上下文环境中有一类Father,在DispatcherServlet 的上下文环境中是可以访问到的。
//通过request获取 应用上下文, MVC 私有bean WebApplicationContext context = RequestContextUtils.getWebApplicationContext(request); //通过ContextLoader获取根容器(必须在web.xml中配置ContextLoaderListener监听) WebApplicationContext contextF = ContextLoader.getCurrentWebApplicationContext();
相关文章推荐
- Android 开发随谈
- iOS 基础知识大全(OC篇零基础可学习)
- Swift - Closure
- [置顶] Unity关于图形学基础知识-矩阵的逆(七)
- SmsMessage类createFromPdu(byte[] pdu)
- Android DialogFragment(1)
- iOS-UIWebView
- Application 统计在线人数
- IOS开发 CocoaPods 使用 pod Install 出现 Updating local specs repositories
- Android监听是否点击了home键或者锁屏键
- iOS开发之简单实现圆形进度条
- swift - map() 与flatMap()
- WebView点击图片看大图效果
- iOS 图标、图形尺寸?
- android对多物理按键,方向键的支持
- 聪明的iOS开发者的Xcode使用技巧
- ios 计算字符串长度<转>
- iOS获取键盘的高度
- iOS --VVDocumenter注释的安装和使用
- iOS --VVDocumenter注释的安装和使用