ContextLoaderListener与DispatcherServlet

来源:互联网 发布:域名访问升级中 编辑:程序博客网 时间:2024/06/06 02:02

如果你的项目里spring都配置好了,但是在JSP里有这么一句:

ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(application);

得到的context为null,那很有可能是你使用DispatcherServlet这种方式配置的

<servlet>    <servlet-name>spring</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>spring</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>

神奇的是,当你换成下面第二种配置方式,它便顺利地拿到了context

<context-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/spring-servlet.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>

悲剧的是,使用第二种方式,你的controller注解可能有问题,我就遇到了,换成第一种方式配置,请求就可以顺利地进入controller的方法里,于是两种配置都要,不仅能进入controller,也能在JSP里拿到context,但是ContextLoaderListener与DispatcherServlet的作用不是一样的吗?都是用来读取spring配置文件并做初始化的,两个都用不就重了?

实际上这两种方式是有区别的。使用第一种方式,也可以拿到context.

ContextLoaderListener中加载的context成功后,spring 将 applicationContext存放在ServletContext中key值为”org.springframework.web.context.WebApplicationContext.ROOT“的attribute中。
可以通过WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
WebApplicationContextUtils.getWebApplicationContext(servletContext)
方法来获取对应的applicationContext。

DispatcherServlet加载的context成功后,如果 publishContext属性的值设置为true的话(缺省为true) 会将applicationContext存放在org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName)的attribute中。
比如:

<servlet>    <servlet-name>spring</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>spring</servlet-name>    <url-pattern>/</url-pattern>  </servlet-mapping>  

则对应的applicationContext的attribute key值为org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring。
在每次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。

ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(application,"org.springframework.web.servlet.FrameworkServlet.CONTEXT.spring");

从上面的分析可以看出,DispatcherServlet所加载的applicationContext可以认为是mvc私有的context,由于保存在servletContext中的key值与通过ContextLoaderListener加载进来的applicationContext使用的key值不相同,因此如果只使用DispatcherServlet加载context的话,如果程序中有地方使用WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) 来试图获取applicationContext时,就会抛出”No WebApplicationContext found: no ContextLoaderListener registered?”的exception,而通过WebApplicationContextUtils.getWebApplicationContext(servletContext)则取不到context,因此默认返回null.

0 0
原创粉丝点击