No WebApplicationContext found: no ContextLoaderListener registered


修改前运行报错:No WebApplicationContext found: no ContextLoaderListener registered? 

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <filter>
      <filter-name>CharacterEncodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
      </init-param>
<!--       <init-param> -->
<!--           <param-name>forceEncoding</param-name> -->
<!--           <param-value>true</param-value> -->
<!--       </init-param> -->
  </filter>
  <filter-mapping>
      <filter-name>CharacterEncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
<!--   配置shiro 过滤器 -->
  <filter>
      <filter-name>shiroFilter</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      <init-param>
          <param-name>targetFilterLifecycle</param-name>
          <param-value>true</param-value>
      </init-param>
      <init-param>
          <param-name>targetBeanName</param-name>
          <param-value>shiroFilter</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>shiroFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
<!--   配置springMVC核心控制器 -->
  <servlet>
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring_config.xml</param-value> </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>DispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

<welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- 配置错误页面 --> <error-page> <error-code>404</error-code> <location>/WEB-INF/jsp/common/error.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/WEB-INF/jsp/common/error.jsp</location> </error-page> <error-page> <error-code>503</error-code> <location>/WEB-INF/jsp/common/error.jsp</location> </error-page> <error-page> <error-code>400</error-code> <location>/WEB-INF/jsp/common/error.jsp</location> </error-page> </web-app>

修改后:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>web</display-name>  
  <filter>
      <filter-name>CharacterEncodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
          <param-name>encoding</param-name>
          <param-value>UTF-8</param-value>
      </init-param>
<!--       <init-param> -->
<!--           <param-name>forceEncoding</param-name> -->
<!--           <param-value>true</param-value> -->
<!--       </init-param> -->
  </filter>
  <filter-mapping>
      <filter-name>CharacterEncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
<!--   配置shiro 过滤器 -->
  <filter>
      <filter-name>shiroFilter</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
      <init-param>
          <param-name>targetFilterLifecycle</param-name>
          <param-value>true</param-value>
      </init-param>
      <init-param>
          <param-name>targetBeanName</param-name>
          <param-value>shiroFilter</param-value>
      </init-param>
  </filter>
  <filter-mapping>
      <filter-name>shiroFilter</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring_config.xml</param-value> </context-param>
  
<!--   配置springMVC核心控制器 -->
  <servlet>
      <servlet-name>DispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring_mvc.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>DispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
      <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
       
  <!-- 配置错误页面 -->
  <error-page>
      <error-code>404</error-code>
      <location>/WEB-INF/jsp/common/error.jsp</location>
  </error-page>
  <error-page>
      <error-code>500</error-code>
      <location>/WEB-INF/jsp/common/error.jsp</location>
  </error-page>
  <error-page>
      <error-code>503</error-code>
      <location>/WEB-INF/jsp/common/error.jsp</location>
  </error-page>
  <error-page>
      <error-code>400</error-code>
      <location>/WEB-INF/jsp/common/error.jsp</location>
  </error-page>
</web-app>

原因分析:

<filter>  
        <filter-name>shiroFilter</filter-name>  
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
        <init-param>  
            <param-name>targetFilterLifecycle</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>shiroFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
        <dispatcher>REQUEST</dispatcher>  
        <dispatcher>FORWARD</dispatcher>  
    </filter-mapping> 

  项目加载的shiro过滤器的功能需要依赖ContextLoaderListener所加载的apring_config.xml文件生成的applicationContext即Spring的上下文,而之前DispatcherServlet所加载的spring_mvc.xml文件生成的applicationContext为Spring mvc的上下文,shiro过滤器无法加载,所以得依靠监听器加载public的applicationContext。

三个context之间的的关系:

  1. 对于一个web应用,web容器加载一个全局context(即servletContext)供其使用,为后面的spring IoC容器提供依赖。
  2. 在web.xml中提供的ContextLoaderListener监听器,web容器启动时会触发容器初始化事件,ContextLoaderListener会监听这个事件,初始化一个根上下文即WebApplicationContext,实现类为XmlWebApplicationContext。这个就是Spring的IoC容器,spring将其以WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE为key存储在servletContext中
  3. DispatcherServlet在初始化会建立自己的IoC容器用以持有Spring mvc相关的bean,它在建立的时候会通过WebApplicationContext.ROOTWEBAPPLICATIONCONTEXTATTRIBUTE先从servletContext中获取根上下文作为自己上下文的父上下文,它的实现类也是XmlWebApplicationContext,在建立之后会以和自己servlet-name便签有关的名称存储在servletContext中,这样每个servlet就拥有自己独立的bean及根上下文共享的bean

  但是这样会导致重复加载配置文件,DispatcherServlet、ContextLoaderListener会分别加载一次,解决方法是把配置文件拆分成2个,一个Spring mvc的配置文件,一个Spring的配置文件,DispatcherServlet加载Spring mvc的配置文件,ContextLoaderListener加载Spring的配置文件 。

Spring mvc在使用上下文的时候会使用DispatcherServlet加载的applicationContext,也会使用的contextLoaderListener加载的applicationContext。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM