Spring ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别

from:
http://blog.csdn.net/madun/article/details/8988860/


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。


补充:

from :http://blog.csdn.net/wym1581/article/details/46467185

ContextLoaderListener初始化的上下文:Bean,配置文件,针对dao,service
DispatcherServlet初始化的上下文加载的Bean是只对spring Web MVC有效的Bean,如Controller、HandlerMapping、HandlerAdapter等等,该初始化上下文应该只加载Web相关组件
DispatcherServlet的容器继承ContextLoaderListener的容器webApplicationContext,这样做可以让dispatcherservlet中要使用的bean可以从webapplicationcontext中获得。
细说dispatcherservlet:
dispatcherServlet初始化的过程具体主要做了两件事:
1初始化spring mvc使用的web上下文,并且可能指定父类容器(contextloaderlistener的根上下文)
2初始化dispatcherservlet使用的策略,如handermapping handeradapter等
dispatcherservlet的默认配置文件:dispatcherservlet.properties,这个配置文件中有一些特殊的bean,这些bean不需要我们注册就可以启动,如果我们注册了这些bean,默认的bean就不会注册了
这个配置文件中特殊的bean列表:
controller
handlermapping(请求到处理器的映射,如果映射成功返回一个handlerexecutionchain对象)
handleradapter(把处理器包装为适配器,是适配器设计模式的应用 )
viewresolver(把逻辑视图名解析为具体的view,如internalresourceviewresolver将逻辑视图名映射为jsp视图)
localresolver(本地化解析,因为spring支持国际化,因此localresolver解析客户端的local信息从而方便进行国际化)
themeresolver(主题解析,通过它来实现一个页面多套风格)
multipartresolver(文件上传解析)
handlerexceptionresolver (处理器异常解析,可以将异常映射到相应的统一错误界面)
requesttoviewnametranslator(当处理器没有逻辑视图名时,自动将请求url映射为逻辑视图名)
flashmapmanager (用于管理flashmap的策略接口,flashmap用于存储一个请求的输出,当进入另一个请求时作为该请求的输入,通常用于重定向场景)

猜你喜欢

转载自angie.iteye.com/blog/2362143