【转】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 app 像 native app 那样运行(雄起吧,Web 开发者)
- JavaWeb_(Spring框架)认识Spring中的aop
- Spring系列.AOP原理简析
- 解决spring的java.lang.IllegalArgumentException异常
- ssm(Spring+Spring mvc+mybatis)——web.xml
- spring boot:创建一个简单的web(maven web project)
- org.springframework.transaction 包改成 spring-tx
- Spring异常解决 java.lang.NullPointerException,配置spring管理hibernate时出错
- 移动WEB应用开发框架moible-web-app开源
- 使用web.xml方式加载Spring时,获取Spring context的两种方式
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
- spring boot properties 编码问题[四]
- spring boot:用dynamic-datasource-spring-boot-starter配置druid多数据源(spring boot 2.3.3)
- spring cloud常用组件介绍
- Scala 2.13.1 整合 Spring Boot 2.2.1开发web应用完整pom文件
- Atitit 部署了个webdav服务 as root 目录 1.1. WEB-INF copy to root dir only a web.xml use...1 1.2. Java.ba
- 【Spring笔记02】Spring中的IOC容器和DI依赖注入介绍
- 搭建Dynamic Web Project(动态web项目)的springmvc工程2
- 高效实现Web开发:掌握Spring Boot 2高级模块Thymeleaf的神奇妙用
- 慕课10、API网关-Spring Cloud Gateway
- 【Web测试】各类web控件测试点汇总,软测人必备
- JavaEE——Spring学习笔记06【Maven创建Web工程】
- JavaEE——Spring学习笔记04【Spring的事务管理】