【转】web.xml中的contextConfigLocation在spring中的作用
一、spring中如何使用多个xml配置文件
1、在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个参数,spring会默认加载WEB-INF/applicationContext.xml文件(若没有,要新建一个)。
例如:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:conf/spring/applicationContext_core*.xml,
classpath*:conf/spring/applicationContext_dict*.xml,
classpath*:conf/spring/applicationContext_hibernate.xml,
</param-value>
</context-param>
contextConfigLocation参数的<param-value>定义了要加载的Spring配置文件。
PS:classpath指编译后的class路径。包含 WEB-INF/lib下的所有jar包和WEB-INF/classes目录
原理:利用ServletContextListener实现
Spring提供ServletContextListener的一个实现类ContextLoaderListener,该类可以作为listener使用,它会在创建时自动查找WEB-INF/下的applicationContext.xml文件。因此,如果只有一个配置文件,并且文件名为applicationContext.xml,则只需要在web.xml文件中增加如下代码即可:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果有多个配置文件载入,则考虑使用<context-param>元素来确定配置文件的文件名。由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数(注意:这是底层代码写死的名称),因此,配置<context-param>时参数名字应该是contextConfigLocation。
带多个配置文件的web.xml文件如下:
<web-app>
<!--确定多个配置文件-->
<context-param>
<!-- 参数名为contextConfigLocation…-->
<param-name>contextConfigLocation</param-name>
<!--多个配置文件之间以,隔开-->
<param-value>/WEB-工NF/daoContext.xml./WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 采用listener创建ApplicationContext 实例-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
如果没有contextConfigLocation参数指定配置文件,则Spring自动查找applicationContext.xml配置文件。如果有contextConfigLocation,则利用该参数确定配置文件。改参数的值指定一个字符串,Spring的ContextLoaderListener负责将该字符串分解成多个配置文件,逗号、空格及分号都可以作为字符串的分割符。如果既没有applicationContext.xml文件,也没有使用contextConfigLocation参数确定配置文件,或者contextConfigLocation确定的配置文件不存在,都将导致Spring无法加载配置文件,从而无法正常创建ApplicationContext实例。
二、使用通配符
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
比如说用到Hibernate,则把hibernate相关的配置放在applicationContext-hibernate.xml这一个文件,而一些全局相关的信息则放在applicationContext.xml中,其它的配置文件类似,这样就不必用空格或逗号分开多个配置文件了。
【转】web.xml中的contextConfigLocation在spring中的作用的更多相关文章
- (转)web.xml中的contextConfigLocation在spring中的作用
(转)web.xml中的contextConfigLocation在spring中的作用 一.Spring如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocat ...
- web.xml中的contextConfigLocation在spring中的作用
在web.xml中通过contextConfigLocation配置spring, contextConfigLocation参数定义了要装入的 Spring 配置文件.默认会去/WEB-INF/下加 ...
- spring web.xml 标签<param-name>contextConfigLocation</param-name>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</lis ...
- web.xml 详解contextConfigLocation 转
spring的应用初始化流程一直没有搞明白,刚刚又碰到了相关的问题.决定得好好看看这个流程.我们在开发spring的项目当中基本上都会在web.xml通过: <context-param> ...
- spring boot 1.x完整学习指南(含各种常见问题servlet、web.xml、maven打包,spring mvc差别及解决方法)
spring boot 入门 关于版本的选择,spring boot 2.0开始依赖于 Spring Framework 5.1.0,而spring 5.x和之前的版本差距比较大,而且应该来说还没有广 ...
- Spring,SpringMvc配置常见的坑,注解的使用注意事项,applicationContext.xml和spring.mvc.xml配置注意事项,spring中的事务失效,事务不回滚原因
1.Spring中的applicationContext.xml配置错误导致的异常 异常信息: org.apache.ibatis.binding.BindingException: Invalid ...
- web.xml的简单解释以及Hello1中web.xml的简单分析
一.web.xml的加载过程 ①当我们启动一个WEB项目容器时,容器包括(JBoss,Tomcat等).首先会去读取web.xml配置文件里的配置,当这一步骤没有出错并且完成之后,项目才能正常的被启动 ...
- 传值:web.xml传递参数 即在Servlet中获取web.xml里的值
传值:web.xml传递参数 在web.xml中的Servlet里配置多个init-param <servlet> ... <init-param> <param-nam ...
- web.xml之<context-param>与<init-param>的区别与作用【转】
引用自-->http://www.cnblogs.com/hzj-/articles/1689836.html <context-param>的作用:web.xml的配置中<c ...
随机推荐
- Linux下网络配置与修改Centos7为列
一.基础知识 手动绑定: 命令 一般是临时的修改,重启后失效,如:ifconfig.route.ip addr等. 修改配置文件 修改文件配置,永久有效,但是可能不能立即生效,需要重启服务 (serv ...
- laravel 预加载特定的列
/**订单列表 0 已删除 1执行中 2 已过期 * * @param Request $request * * @return \Illuminate\Contracts\View\Factory| ...
- Windows服务器上使用phpstudy部署PHP程序
一.下载并安装PHPStudy 官网地址:http://phpstudy.php.cn/(安装包下载地址:链接:https://pan.baidu.com/s/1WOmbOwmLuUPt3_nmY6- ...
- Spring Boot 2.x 编写 RESTful API (五) 单元测试
用Spring Boot编写RESTful API 学习笔记 概念 驱动模块 被测模块 桩模块 替代尚未开发完毕的子模块 替代对环境依赖较大的子模块 (例如数据访问层) 示例 测试 Service @ ...
- ezdxf包下autocad的开发
利用 ezdxf 库读写geotiff格式的地形影像方法(InsertRasterToCAD) 包一:dxfgrabber GitHub - mozman/dxfgrabber: Outdated D ...
- 「Algospot」量化QUANTIZE
一道不难的DP题,主要是为了总结这类最优化题的思路:同时还学到了一个新操作 传送门:$>here<$ 题意 给出一个长度为$N$的序列,要求最多使用s个数字进行量化(有损压缩),即代替原数 ...
- 【linux】工作中linux系统常用命令操作整理
1.Linux如何查看端口 使用lsof(list open files)命令,lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000. 或者使用n ...
- python之路day08--文件的操作
文件的操作 hanfei的博客.txt 1/文件的路径2.编码方式3.操作方式:只读,只写,追加,读写,写读... 只读 f=open('hanfei的博客',mode='r',encoding='u ...
- 从redis中取值如果不存在设置值,使用Redisson分布式锁【我】
用到的jar包: <!-- Redis客户端 --> <dependency> <groupId>redis.clients</groupId> < ...
- Linux记录-GC值
jmap -heap pid 查看gc情况: jstat -gc PID 刷新频率 jstat -gc 12538 5000 导出堆内存dump 文件: jmap -dump:file=文件名.bin ...