Spring org.springframework.web.context.ContextLoaderListener

public class ContextLoaderListener

extends Objectimplements ServletContextListener

作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息。

因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。

看看ContextLoader的API说明

第一段说明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。如果查看ContextLoaderServlet的API,可以看到它也关联了ContextLoader这个类而且它继承了HttpServlet

第二段,ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->

BeanFactory这样一来spring中的所有bean都由这个类来创建

第三段,讲如何部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext-*.xml
</param-value>
</context-param>

在<param-value> </param-value>里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都会一同被载入。

-----------------------------------------------------------------------------------------

例子,在web.xml文件中加入下面代码

<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

如果applicationContext.xml文件没有在/WEB-INF/下,或文件名不一致,或存在多个Spring配置文件,在web.xml文件中根据下面代码修改

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:applicationContext-*.xml,/WEB-INF/applicationContext.xml,/WEB-INF/classes/applicationContext-*.xml
</param-value>
  </context-param>

Spring之ContextLoaderListener的作用的更多相关文章

  1. spring项目中监听器作用-ContextLoaderListener(转)

    1 spring框架的启动入口 ContextLoaderListener 2 作用:在启动Web 容器时,自动装配Spring applicationContext.xml 的配置信息. 因为它实现 ...

  2. spring cloud 各子项目作用

    spring cloud 各子项目作用: table th:first-of-type { width: 80px; } table th:nth-of-type(2) { width: 150px; ...

  3. 【转】详解spring 每个jar的作用

    spring.jar 是包含有完整发布模块的单个jar 包.但是不包括mock.jar, aspects.jar, spring-portlet.jar, and spring-hibernate2. ...

  4. Spring 中bean的作用、定义

    Spring 中bean的作用.定义: 创建一个bean定义,其实质是用该bean定义对应的类来创建真正实例的"配方(recipe)".把bean定义看成一个配方很有意义,它与cl ...

  5. spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on

    spring,spring mvc之所以起作用是因为开启了注解解释器,即spring的annotation on

  6. [Java][Spring]Spring事务不起作用 问题汇总

    [Java][Spring]Spring事务不起作用 问题汇总 http://blog.csdn.net/szwangdf/article/details/41516239

  7. spring项目中监听器作用-ContextLoaderListener(项目启动时,加载一些东西到缓存中)

    作用:在启动Web容器时,自动装配Spring applicationContext.xml的配置信息. 因为它实现了ServletContextListener这个接口,在web.xml配置这个监听 ...

  8. spring项目中监听器作用-ContextLoaderListener

    附加链接:http://blog.csdn.net/zjw10wei321/article/details/40145241 作用:在启动Web 容器时,自动装配Spring applicationC ...

  9. spring配置文件默认名称及位置,ContextLoaderListener监听器作用

    spring在web.xml中的配置 由于spring需要启动容器才能为其他框架提供服务,而web应用程序的入口是由web服务器控制的,因此无法在main()方法中通过创建ClassPathXmlAp ...

随机推荐

  1. Java的23种设计模式

    1.FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯 德基,只管向服务员说“来四个鸡翅”就行了.麦当劳和肯德基就是生产鸡 ...

  2. 使用C#开发ActiveX控件

    使用C#开发ActiveX控件(新) 前言 ActiveX控件以前也叫做OLE控件,它是微软IE支持的一种软件组件或对象,可以将其插入到Web页面中,实现在浏览器端执行动态程序功能,以增强浏览器端的动 ...

  3. fastboot,bootloader,recovery

    Fastboot模式,在这个模式下,我们可以为手机进行解锁.可以在该模式下连接电脑进行软件或者指令解锁(s-off),这个模式也称“下载模式” Fastboot fastboot是一种比recover ...

  4. head first--------------------template method pattern

    head first---------模板方法模式         浅谈谈模板方法模式:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中.模板方法使得子类可以在不改变算法结构的情况下,重新定义 ...

  5. VMware vSphere 服务器虚拟化之二十四 桌面虚拟化之手动池管理物理机

    VMware vSphere 服务器虚拟化之二十四 桌面虚拟化之手动池管理物理机 VMwareView手动池可以管理物理计算机 说明: 环境基于实验二十三 1.准备一台Windows 7的物理计算机名 ...

  6. poj 1088 滑雪(贪心算法)

    思想: (贪心算法 ,看到题目是中文才做的) 先对数组中的数据进行排序,从最小的数据计算 当前的顶点的可以滑行的最大值=max(周围可达的顶点的可以滑行的最大值)+1 这样计算最后产生的路径肯定是最大 ...

  7. 以helloworld为例讲解magento中控制器的工作

    1.下面介绍的前提是你已经安装了magento ,版本是1.9.1.0. 2.下面是实际步骤 ①在工程下面创建下面的文件目录 app/code/local/Magentotutorial/Hellow ...

  8. jQuery2.x源码解析(DOM操作篇)

    jQuery2.x源码解析(构建篇) jQuery2.x源码解析(设计篇) jQuery2.x源码解析(回调篇) jQuery2.x源码解析(缓存篇) jQuery这个类库最为核心重要的功能就是DOM ...

  9. Cocos2d-x 截图功能

    2.x-3.x版本                 //获取屏幕尺寸         CCSize size = CCDirector::sharedDirector()->getWinSize ...

  10. scrapy在ubuntu上安装总结

    此文档是本人学习时使用的,采用一个实例作为引导进行安装测试. 实例下载地址如下: https://github.com/sans-serif/scrapy-german-news#introducti ...