spring applicationContext.xml 中context:property-placeholder
来源:互联网 发布:大富豪5.2.1最新源码 编辑:程序博客网 时间:2024/05/23 15:47
spring applicationContext.xml 中context:property-placeholder
一.
1.有些参数在某些阶段中是常量
比如 :a、在开发阶段我们连接数据库时的连接url,username,password,driverClass等b、分布式应用中client端访问server端所用的server地址,port,service等
c、配置文件的位置
2.而这些参数在不同阶段之间又往往需要改变
比如:在项目开发阶段和交付阶段数据库的连接信息往往是不同的,分布式应用也是同样的情况。
期望:能不能有一种解决方案可以方便我们在一个阶段内不需要频繁书写一个参数的值,而在不同阶段间又可以方便的切换参数配置信息
解决:spring3中提供了一种简便的方式就是context:property-placeholder/元素,只需要在spring的配置文件里添加一句:<context:property-placeholder
location="classpath:jdbc.properties"/> 即可,这里location值为参数配置文件的位置,参数配置文件通常放在src目录下,而参数配置文件的格式跟java通用的参数配置文件相同,即键值对的形式,
例如:
#jdbc配置<span style="color:#ff0000;">test.jdbc.driverClassName</span>=com.mysql.jdbc.Drivertest.jdbc.url=jdbc:mysql://localhost:3306/testtest.jdbc.username=roottest.jdbc.password=root
行内#号后面部分为注释
应用:
1.这样一来就可以为spring配置的bean的属性设置值了,比如spring有一个jdbc数据源的类DriverManagerDataSource
在配置文件里这么定义bean:
<bean id="testDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${test.jdbc.driverClassName}"/>
<property name="url" value="${test.jdbc.url}"/>
<property name="username" value="${test.jdbc.username}"/>
<property name="password" value="${test.jdbc.password}"/>
</bean>
2.甚至可以将${ }这种形式的变量用在spring提供的注解当中,为注解的属性提供值
二.遇到的问题
如果遇到下面着着这种问题:
A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件:
A模块的Spring配置文件如下:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:property-placeholder location="classpath*:conf/conf_a.properties"/> <bean class="com.xxx.aaa.Bean1" p:driverClassName="${modulea.jdbc.driverClassName}" p:url="${modulea.jdbc.url}" p:username="${modulea.jdbc.username}" p:password="${modulea.jdbc.password}"/> </beans>
conf/conf_a.properties:
modulea.jdbc.driverClassName=com.mysql.jdbc.Driver modulea.jdbc.username=cartan modulea.jdbc.password=superman modulea.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8B模块的Spring配置文件如下:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:property-placeholder location="classpath*:conf/conf_b.properties"/> <bean class="com.xxx.bbb.Bean1" p:driverClassName="${moduleb.jdbc.driverClassName}" p:url="${moduleb.jdbc.url}" p:username="${moduleb.jdbc.username}" p:password="${moduleb.jdbc.password}"/> </beans>
conf/conf_b.properties:
moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver moduleb.jdbc.username=cartan moduleb.jdbc.password=superman moduleb.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8
问题:单独运行A模块,或单独运行B模块都是正常的,但将A和B两个模块集成后运行,Spring容器就启动不了:
Could not resolve placeholder 'moduleb.jdbc.driverClassName' in string value "${moduleb.jdbc.driverClassName}"
原因:Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描
spring容器中最多只能定义一个context:property-placeholder,不然就出现那种个错误,那如何来解决上面的问题呢?
A和B模块去掉
<context:property-placeholder location="classpath*:conf/conf_a.properties"/>
<context:property-placeholder location="classpath*:conf/conf_b.properties"/>
然后重新写个xml:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <span style="color:#ff6666;"><strong> <context:property-placeholder location="classpath*:conf/conf*.properties"/> </strong></span> <import resource="a.xml"/> //对应着A模块的配置文件 <import resource="b.xml"/> //对应着B模块的配置文件 </beans>
0 0
- spring applicationContext.xml 中context:property-placeholder
- spring中context:property-placeholder
- spring中context:property-placeholder/
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder元素
- spring中context:property-placeholder/元素 (
- spring中context:property-placeholder/元素
- spring中context:property-placeholder元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- spring中context:property-placeholder解析
- spring中context:property-placeholder/元素
- spring中context:property-placeholder/元素
- 什么才是你的目标
- Android 百度地图开发(三)
- 微信支付-公众号支付H5调用支付详解
- mysql中int、bigint、smallint 和 tinyint的区别
- 算法分析与设计实验 动态规划法 求最长公共子序列
- spring applicationContext.xml 中context:property-placeholder
- Swift 如何实现手势识别,单点 长按 旋转 放大
- Effective C++学习笔记(2)
- 百度地图(四)
- HDOJ 5215 Cycle
- 九寨沟出租车公司——九寨沟旅游服务中心为您证实
- 初次安装RED HAT下与在该系统下安装db2 9.7所遇到的问题
- 大焕视界:乡愁是一道深不可测的陷阱
- 关于 Azure 存储帐户(1)