先来看下A和B两个模块
A模块和B模块都分别拥有本身的Spring XML配置,并分别拥有本身的配置文件:
A模块
A模块的Spring配置文件以下:
mysql
<?xml version="1.0" encoding="UTF-8" ?> sql
<beans xmlns="http://www.springframework.org/schema/beans" app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" url
xmlns:context="http://www.springframework.org/schema/context" spa
xmlns:p="http://www.springframework.org/schema/p" code
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd xml
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> get
<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模块
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容器就启动不了了:
引用
Could not resolve placeholder 'moduleb.jdbc.driverClassName' in string value "${moduleb.jdbc.driverClassName}"
到底出了啥问题
随便搜索了一下,还发现不少人遇到这个问题,这个就是来自stackoverflow的问题:
http://stackoverflow.com/questions/7940452/spring-application-context-not-able-to-load-property-placeholder-properties
惋惜啊,好像都没有人给出正确的解决。
那到底是什么问题呢?也想了好久哦....终于回想起来了(写书时读过Spring源码),原来是Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会中止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。
而<context:property-placeholder/>这个基于命名空间的配置,其实内部就是建立一个PropertyPlaceholderConfigurer Bean而已。换句话说,即Spring容器仅容许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其他的会被Spring忽略掉(其实Spring若是提供一个警告就行了)。
拿上来的例子来讲,若是A和B模块是单独运行的,因为Spring容器都只有一个PropertyPlaceholderConfigurer,所以属性文件会被正常加载并替换掉。若是A和B两模块集成后运行,Spring容器中就有两个PropertyPlaceholderConfigurer Bean了,这时就看谁先谁后了, 先的保留,后的忽略!所以,只加载到了一个属性文件,于是形成没法正确进行属性替换的问题。
咋解决呢?
定位问题须要9999元钱,解决问题只须要1元钱 。
属性文件加载在统一的地方作,不要分模块加载便可。
A模块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这样作是利大于弊的。