ContextLoaderListener作用详解
来源:互联网 发布:淘宝宝贝创建时间 编辑:程序博客网 时间:2024/06/08 16:56
ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
配置:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
配合它一起使用的,经常是context-param,用来指定Spring要加载的配置文件,比如:
<!-- 加载Spring容器 --><context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value></context-param>
部署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等文件,都会一同被载入。
由此可见applicationContext.xml的文件位置就可以有两种默认实现:
第一种:直接将之放到/WEB-INF下,只在web.xml中声明一个listener;
第二种:将之放到classpath下,但是此时要在web.xml中加入,用它来指明你的applicationContext.xml的位置以供web容器来加载。
更多学习交流请加群(非诚勿扰):
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- ContextLoaderListener作用详解
- Bootstrap模版框架怎么用
- 10款超好用的开源大数据分析工具
- Struts2 + spring 环境搭建
- 欢迎使用CSDN-markdown编辑器
- 【区间dp】Treats for the Cows POJ
- ContextLoaderListener作用详解
- 开源OkHttpUtils升级版OkGo,完美支持RxJava
- linux中的genewise怎么安装
- 老毛桃装系统
- C++将string转换为char
- 【Trie(字典树)--模板】学习
- <script>标签的for属性和event属性
- POJ
- log4j2.8和springboot搭配使用时的xwEx问题