【转】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 ...
随机推荐
- Promise async-await 异步解决方案
1.简介: async和await在干什么,async用于申明一个function是异步的,而await可以认为是async wait的简写,等待一个异步方法执行完成. 2.基本语法 在C ...
- Linux(Ubuntu)使用日记------Mysql编码(utf-8)的设置
Mysq版本:5.7.21 操作系统:Linux(Ubuntu) 整个操作的基本思路如下(包括问题的解决思路,想要直接解决问题的可以先看最后的命令总结) 检查mysql编码 找到Mysql的配置文件 ...
- Python视频人脸检测识别
案例 这里我们还是使用 opencv 中自带了 haar人脸特征分类器,通过读取一段视频来识别其中的人脸. 代码实现: 动图有点花,讲究着看吧: 如果是捕捉摄像头,只需要改变以下代码即可: c ...
- win 执行puppet
C:\scripts\win_exec_proxy.bat \\adsoft.base-fx.com\puppet\puppet\files\Windows_10_x64\C\user\logon\ ...
- CSS绝对定位元素居中的几种方法
转载自-CSS居中绝对https://www.cnblogs.com/skura23/p/6530556.html 作者:PajamaCat 1,div宽度未知1 <body> <d ...
- 注解 springbootapplication 自动扫描所在包及其子包。会将有注解的加入到spring容器中
注解 springbootapplication 自动扫描所在包及其子包.会将有注解的加入到spring容器中
- [题解]小X的液体混合
版权说明:来自 石门ss学校 Guohao OJ ,禁止转载 题目描述 虽然小X不喜欢化学原理,但他特别喜欢把一大堆液体倒在一起. 现在小X有n种液体,其中m对会发生反应.现在他想把这n种液体按某种顺 ...
- ezdxf包下autocad的开发
利用 ezdxf 库读写geotiff格式的地形影像方法(InsertRasterToCAD) 包一:dxfgrabber GitHub - mozman/dxfgrabber: Outdated D ...
- cinder-volume报错vmdk2 is reporting problems, not sending heartbeat. Service will appear "down".
cinder-volume报错vmdk2 is reporting problems, not sending heartbeat. Service will appear "down&qu ...
- LOJ 6281 数列分块入门 5
简化版题意 给出一个长为n的数列,以及n个操作,操作涉及区间开方(每个数都向下取整),区间求和,保证所有数都为有符号32位正整数. N<=50000 Solution 首先我们先思考: 一个有符 ...