先来看下A和B两个模块,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=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容器就启动不了了:

Could not resolve placeholder 'moduleb.jdbc.driverClassName' in string value "${moduleb.jdbc.driverClassName}"

原因是。

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了,这时就看谁先谁后了, 先的保留,后的忽略!因此,只加载到了一个属性文件,因而造成无法正确进行属性替换的问题。

解决方法。

属性文件加载在统一的地方做,不要分模块加载即可。

A模块a.application.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.application.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>
 

最新文章

  1. html: title换行方法 如a链接标签内title属性鼠标悬停提示内容换行
  2. 使用UltraEdit实现从DOS文件到UNIX文件的批量转换
  3. stream_copy_to_stream的使用
  4. HDU1429+bfs+状态压缩
  5. 隐式Intent实例
  6. 013实现使用两个堆栈队列(keep it up)
  7. netcat工具的使用
  8. ora-01440:要减小精度或标度,则要修改的列必须为空
  9. 永久开启完整版Google Play
  10. Scyther-Semantics and verification of Security Protocol
  11. java中的static代码块为什么只执行一次
  12. Springboot自定义过滤器Filter
  13. Linux学习期中总结
  14. luogu2149 Elaxia的路线 (dijkstra+拓扑dp)
  15. PowerTCP FTP for .NET 在线e文文档
  16. C++进阶--RAII 资源获取即初始化
  17. C++语言定义的标准转换
  18. Celery -- 分布式任务队列 及实例
  19. dip和px的相互转化
  20. PHP 生成Word文档

热门文章

  1. BZOJ 2716 [Violet 3]天使玩偶 (CDQ分治、树状数组)
  2. node、npm、git版本升级
  3. centos64位编译32位程序
  4. zabbix监控惠普打印机
  5. shell脚本之结构化命令if...then...fi
  6. ECMA Script 6新特性之解构赋值
  7. Git-Runoob:Git 教程
  8. 看看 Delphi XE2 为 VCL 提供的 14 种样式
  9. 八:flask-重定向示例
  10. 《Using Python to Access Web Data》Week4 Programs that Surf the Web 课堂笔记