Spring配置:用context:property-placeholder替换PropertyPlaceholderConfigurer
1、有时候需要从properties文件中加载配置,以前的方式是这样的:
- <bean id="jdbcProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="locations">
- <list>
- <value>classpath*:/spring/jdbc.properties</value>
- </list>
- </property>
- </bean>
spring推荐使用如下方式配置:
- <context:property-placeholder location="classpath:spring/jdbc.properties" />
以上两种方式,在bean定义中依然可以通过“${}”这种方式来去值:
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="${jdbc.driverClassName}" />
- <property name="url" value="${jdbc.url}" />
- <property name="username" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
注:<context:property-placeholder/>这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。
2、关于<context:property-placeholder>的一个有趣现象
先来看下A和B两个模块 ,A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件:
A模块 ,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=utf8
B模块的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容器就启动不了了:
原因:
原来是Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。 换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)。
拿上来的例子来说,如果A和B模块是单独运行的,由于Spring容器都只有一个PropertyPlaceholderConfigurer,因此属性文件会被正常加载并替换掉。如果A和B两模块集成后运行,Spring容器中就有两个PropertyPlaceholderConfigurer Bean了,这时就看谁先谁后了, 先的保留,后的忽略!因此,只加载到了一个属性文件,因而造成无法正确进行属性替换的问题。
解决:
A模块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">
- <!--<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>
B模块b.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">
- <!--<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>
集成:
- <?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*.properties"/>
- <import resource="a.xml"/>
- <import resource="b.xml"/>
- </beans>
Spring为什么要这样呢?细想想是有道理的,一个项目或一个系统的配置应该放在一起,不宜分散。
这样才可以做到统一管控,否则到处都有配置,到底是加载哪个配置文件呢?有时你还会不小心让JAR中的Spring配置文件加载一个位于JAR中的属性文件,而外面有更改不了。如果Spring使用了这种机制,即使JAR包中的Spring配置文件使用<context:property-placeholder/>引用到JAR中的属性文件,只要你要外而的Spring配置文件中显示提供一个<context:property-placeholder/>指定另一个属性文件 ,就可以覆盖JAR中的默认配置了。
Spring配置:用context:property-placeholder替换PropertyPlaceholderConfigurer的更多相关文章
- Spring 配置 Annotation <context:annotation-config> 和 <context:component-scan>标签的诠释及区别
Spring 开启Annotation <context:annotation-config> 和 <context:component-scan>诠释及区别 <cont ...
- spring配置注解context:annotation-config和context:component-scan区别
Spring 中在使用注解(Annotation)会涉及到< context:annotation-config> 和 < context:component-scan>配置, ...
- spring配置:context:property-placeholder 读取配置文件信息 在配置文件中使用el表达式填充值
spring将properties文件读取后在配置文件中直接将对象的配置信息填充到bean中的变量里. 原本使用PropertyPlaceholderConfigurer类进行文件信息配置.Prope ...
- Spring配置中<context:annotation-config> VS <context:component-scan>
Spring 中在使用注解(Annotation)会涉及到< context:annotation-config> 和 < context:component-scan>配置, ...
- Spring配置之context:annotation与、component-scan以及annotation-driven
spring boot帮助我们隐藏了大量的细节,有些配置spring boot都是开启的,因此当我们查看遗留项目使用spring时候遇见问题一定细心排查 <!-- context:annotat ...
- spring错误:<context:property-placeholder>:Could not resolve placeholder XXX in string value XXX
spring同时集成redis和mongodb时遇到多个资源文件加载的问题 这两天平台中集成redis和mongodb遇到一个问题 单独集成redis和单独集成mongodb时都可以正常启动程序,但是 ...
- [key]严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener(Spring配置异常)
详细错误为: 严重: Exception sending context initialized event to listener instance of class org.springframe ...
- Spring 出现Could not resolve placeholder问题的解决方法
项目开发中,使用@value注解获取不到配置文件里面的属性字段. 检查配置文件,在spring的配置文件中有配置读取,如下: <!-- 使用spring自带的占位符替换功能 --> < ...
- Spring(十):Spring配置Bean(三)Bean的作用域、使用外部属性文件
Bean的作用域: 支持四种配置,分别是singleton,prototype,request,session. singleton 默认情况下在spring confinguration xml文件 ...
随机推荐
- Web Service自动化测试知识点导图
- finereport点击图表钻取到明细表包括参数传递
1. 点击编辑图表 2. 参数传递 3. 选择分类名称 4. 钻取明细表获取 inputs 值得方法 使用公司 $inputs 获取钻取传来的值
- 【VS开发】使用WinPcap编程(1)——获取网络设备信息
pcap_if_t是一个interface数据结构,表明网络接口的信息.网络接口就是interface,就是我们用来上网的设备,一般为网卡,还有一些虚拟网卡也算作这样的接口.它的结构如下: struc ...
- 【嵌入式 Linux文件系统】如何使用Initramfs文件系统
(1)#cd ../rootfs/ #ln -s ./bin/busybox init 创建软链接 (2)进入Linux内核 #make menuconfig General setup-->I ...
- java基础笔记(11)
css 样式的设置主要有选择器+声明{}:声明里又分为属性和值: 注释代码:/*注释语句*/ 内联式:写在元素开始的标签里:例:<p style = "color:red;font-s ...
- python 发送kafka
python 发送kafka大体有三种方式 1 发送并忘记(不关注是否正常到达,不对返回结果做处理) 1 import pickle 2 import time 3 from kafka import ...
- TScreen研究(有待研究)
先扔在这里,待研究: http://blog.csdn.net/lailai186/article/details/8141170 procedure TForm1.Button1Click(Send ...
- Springboot+Jedis+Ehcache整合
项目结构概览: 1. 导包 <parent> <groupId>org.springframework.boot</groupId> <artifactId& ...
- windows 桌面背景设置实例
应用SystemParametersInfo函数可以获取和设置数量众多的windows系统参数.这个小程序就是运用了SystemParametersInfo函数来设置桌面的墙纸背景,而且程序可以让我们 ...
- spark复习笔记(1)
使用spark实现work count ---------------------------------------------------- (1)用sc.textFile(" &quo ...