修改前运行报错: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>

原因分析:

  项目加载的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。

No WebApplicationContext found: no ContextLoaderListener registered的更多相关文章

  1. No WebApplicationContext found: no ContextLoaderListener registered?报错解决

    今天跑了下新搭的一个SSI框架. 报例如以下错误: 严重: Exception sending context initialized event to listener instance of cl ...

  2. 使用shiro 框架 报错No WebApplicationContext found: no ContextLoaderListener or DispatcherServlet registered?

    1.问题描述:ssm 框架中使用shiro  中出现问题 原来web.xml 文件如下: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, ...

  3. 【转】ContextLoaderListener 和 DispatcherServlet

    转载地址: http://www.guoweiwei.com/archives/797 DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供S ...

  4. ContextLoaderListener与DispatcherServlet所加载的applicationContext的区别

    spring通过在web.xml 中配置ContextLoaderListener 来加载context配置文件,在DispatcherServlet中也可以来加载spring context配置文件 ...

  5. 获取WebApplicationContext的几种方式

    加载WebApplicationContext的方式 WebApplicationContext是ApplicationContext的子接口,纵观Spring框架的几种容器,BeanFactory作 ...

  6. ContextLoaderListener和Spring MVC中的DispatcherServlet学习

    DispatcherServlet介绍 DispatcherServlet是Spring前端控制器的实现,提供Spring Web MVC的集中访问点,并且负责职责的分派,与Spring IoC容器无 ...

  7. ContextLoaderListener和Spring MVC中的DispatcherServlet加载内容的区别【转】

    原文地址:https://blog.csdn.net/py_xin/article/details/52052627 ContextLoaderListener和DispatcherServlet都会 ...

  8. 关于在filter中获取WebApplicationContext的实践

    网上很多说法,诸如: <param-name>contextConfigLocation</param-name> <param-value> classpath: ...

  9. WebApplicationContext初始化的两种方式和获取的三种方式

    原博客地址:http://blog.csdn.net/lmb55/article/details/50510547 接下来以ContextLoaderListener为例,分析它到底做了什么? app ...

随机推荐

  1. JavaScript003,用法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. POJ-2115-C Looooops(线性同余方程)

    链接: https://vjudge.net/problem/POJ-2115 题意: A Compiler Mystery: We are given a C-language style for ...

  3. Spark mllib 随机森林算法的简单应用(附代码)

    此前用自己实现的随机森林算法,应用在titanic生还者预测的数据集上.事实上,有很多开源的算法包供我们使用.无论是本地的机器学习算法包sklearn 还是分布式的spark mllib,都是非常不错 ...

  4. [NOIP 2018]旅行

    题目链接 题意简介:现有一个图,小Y要把它走完,每个点只去一次,路径字典序最小. 分析:这道题我认为很重要的一个点就是它的数据范围.它只有两种 m=n-1 或 m=n.我们先考虑第一种:m=n-1也就 ...

  5. MongoDB Wiredtiger存储引擎实现原理

    Mongodb-3.2已经WiredTiger设置为了默认的存储引擎,最近通过阅读wiredtiger源代码(在不了解其内部实现的情况下,读代码难度相当大,代码量太大,强烈建议官方多出些介绍文章),理 ...

  6. 四十三.MongoDB副本集 MongoDB文档管理

    一.部署MongoDB副本集 1.1 启用副本集配置并指定集群名称 rs1 1.2 定义集群成员列表 部署好机器51,52,53:51上配置 bind_ip=192.168.4.51(要改) port ...

  7. 使用 ServerSocket 建立聊天服务器-1

    1.代码目录 2.ChatSocket.java --------------------------------------------------------------------------- ...

  8. iSCSI引入FC/SAN

    由 cxemc 在 2013-9-24 上午9:10 上创建,最后由 cxemc 在 2013-9-24 上午9:10 上修改 版本 1 集成iSCSI 和FC SAN有五种常见的方法,各有优缺,适应 ...

  9. package.json 与 package-lock.json 的区别

    根据官方文档,这个package-lock.json 是在 `npm install`时候生成一份文件,用以记录当前状态下实际安装的各个npm package的具体来源和版本号. 它有什么用呢?因为n ...

  10. 3、spark Wordcount

    一.用Java开发wordcount程序 1.开发环境JDK1.6 1.1 配置maven环境 1.2 如何进行本地测试 1.3 如何使用spark-submit提交到spark集群进行执行(spar ...