网上找了一下关于ContextLoaderListener和ServletDispatcher的解释,这是原文

http://simone-folino.blogspot.com/2012/05/dispatcherservlet-vs.html

http://syntx.co/languages-frameworks/difference-between-loading-context-via-dispatcherservlet-and-contextloaderlistener/

总结如下:

Spring中有两种上下文环境-"Application Context和Web Application Context",他们分别对应ContextLoaderListener和ServletDispatcher,且都可以用来配置bean的注入,装配,和AOP

1. ContextLoaderListener

ContextLoaderListener通过读取contextConfigLocation参数来读取配置参数,一般来说它配置的是Spring项目的中间层,服务层组件的注入,装配,AOP.

对应到Spring的自动装配机制<context:component-scan>就是以下几种注解的装配

  • DAO: such as @Repository bean
  • Entity: such as @Entity bean
  • Service: such as @Service bean

(以上内容来自引用博文)

下面是一个典型web.xml中加载ContextLoaderListener的代码片段

    <!-- 定义参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/config/applicationContext.xml</param-value>
</context-param> <!-- 定义Listener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

以及 applicationContext.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:annotation-config /> <tx:annotation-driven proxy-target-class="true" transaction-manager="transactionManager" /> <context:component-scan base-package="com.simonefolinoblogspot.dao" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> ... </beans>

可以看到ContextLoaderListener的配置文件都是中间层,数据层,DAO之类的

2. ServletDispatcher

ServletDispatcher通过读取<servlet-name>-servlet.xml文件来读取配置,如果文件不存在,则可以通过servlet标签内的

        <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/config/dispatcher-servlet.xml</param-value>
</init-param>

来指定配置文件,它配置的是Web层组件的注入,装配和AOP

  • Controllers
  • ViewResolvers
  • LocaleResolvers
  • ThemeResolvers

下面是一个典型配置web.xml

<servlet>
<servlet-name>addressbook</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
"> <context:annotation-config /> <context:component-scan base-package="com.simonefolinoblogspot.controller" >
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan> <bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean> </beans>

说一个我的例子,我在Conroller类上加上了@Controller自动注册注释,然后把aop的配置引入放到了applicationContext.xml里,这个配置由ContextLoaderListener读取的

    <import resource="classpath*:spring/aop/aopConfig.xml" />

结果AOP方法就不执行了,解决方法就是需要将aopConfig.xml放到ServletDispatcher的配置xml文件里

ContextLoaderListener 与 ServletDispatcher的更多相关文章

  1. Tomcat启动报错org.springframework.web.context.ContextLoaderListener类配置错误——SHH框架

    SHH框架工程,Tomcat启动报错org.springframework.web.context.ContextLoaderListener类配置错误 1.查看配置文件web.xml中是否配置.or ...

  2. DispatcherServlet 和 ContextLoaderListener 的关系,到底用哪个?

    我们先看下这两个东东的配置方法: 对于contextConfigLocation参数,有2个地方可以配置: 1)context-param 是全局性配置 2)servlet下的init-param 是 ...

  3. maven 项目启动tomcat报错 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    maven项目启动tomcat报错: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderLi ...

  4. Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanCreationException:

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  5. IDEA +maven+ ContextLoaderListener not find

    tomcat 启动失败:SEVERE: Context [] startup failed due to previous errors 查看pox.xml 有spring-web依赖 查看tomca ...

  6. java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    参考: http://www.cnblogs.com/sunxucool/archive/2013/06/07/3124380.html   ---------------------------&g ...

  7. 真正解决问题:maven eclipse tomcat java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    在使用eclipse进行整合springmvc时经常会碰到这样的异常: java.lang.ClassNotFoundException:org.springframework.web.context ...

  8. [Spring框架] Spring中的 ContextLoaderListener 实现原理.

    前言: 这是关于Spring的第三篇文章, 打算后续还会写入AOP 和Spring 事务管理相关的文章, 这么好的两个周末 都在看code了, 确实是有所收获, 现在就来记录一下. 在上一篇讲解Spr ...

  9. Webwork 学习之路【03】核心类 ServletDispatcher 的初始化

    1. Webwork 与 Xwork 搭建环境需要的的jar 为:webwork-core-1.0.jar,xwork-1.0.jar,搭建webwork 需要xwork 的jar呢?原因是这样的,W ...

随机推荐

  1. entityframework删除时提示“DELETE 语句与 REFERENCE 约束”的解决方法。

    1.加入List对象的Include var entity = db.XinDes.Include("Reviews").First(); db.XinDes.Remove(ent ...

  2. Apache配置实现日志按天分割并删除指定几天前的日志

    Apache日志默认情况下是一周切割一次,由于访问量大的时候日志的文件还是比较大的,同时也不利于管理员对日志的分析处理.于是尝试对Apache日志设置按天分割,并通过计划任务执行删除几天的日志. 配置 ...

  3. Python爬虫-request的用法

    import requests if __name__ == '__main__': #基本用法 #response = requests.get("http://httpbin.org/g ...

  4. eclipse maven 配置

    http://www.cnblogs.com/little-YTMM/p/5970878.html

  5. elementUI 学习入门之 input 输入框

    基础用法 <el-input v-model="input1" palcehoder="请输入"></el-input> var Mai ...

  6. java 数组冒泡排序、转置(降序)

    1.java 数组冒泡排序 排序的基本原理(升序): 原始数据:  2 .1 .9 .0 .5 .3 .7 .6 .8: 第一次排序: 1  .2 .0 .5 .3 .7 .6 .8 .9 : 第二次 ...

  7. ref和out的用法和区别。

    关于ref和out的用法和区别在网上已经有很多的解释,这里只不过是写下对于我而说比较容易理解的解释. ref和out都可以用来在函数中返回数据,类似于c++中指针. 参数 Ref Out 是否一定需要 ...

  8. js基本数据类型 BigInt 和 Number 的区别

    今天在做LeetCode的一到 “加一” 的题,题目如下 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除 ...

  9. hibernate4日志配置

    在平时运行程序时控制台有很对的日志打印,本文说明如何控制这些日志打印. 1.确定要使用日志的实现,我在这使用的是log4j. 2.加入log4j的api包slf4j-api.jar,log4j的转换包 ...

  10. C++ STL 全排列函数详解

    一.概念 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n!个. 比如a ...