spring多个context:property-placeholder不生效问题

时间:2025-02-13 14:44:01

先来看下A和B两个模块,A模块和B模块都分别拥有自己的Spring XML配置,并分别拥有自己的配置文件: 

A模块的Spring配置文件如下: 

<?xml version="1.0" encoding="UTF-8" ?>  
<beans xmlns="/schema/beans"  
       xmlns:xsi="http:///2001/XMLSchema-instance"  
       xmlns:context="/schema/context"  
       xmlns:p="/schema/p"  
       xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.  
       /schema/context /schema/context/spring-context-3.">  
   <context:property-placeholder location="classpath*:conf/conf_a.properties"/>  
   <bean class=".Bean1"  
          p:driverClassName="${}"  
          p:url="${}"  
          p:username="${}"  
          p:password="${}"/>  
</beans>  

其配置文件位于类路径conf/conf_a.properties中: 

=  
=cartan  
=superman  
=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8

B模块的Spring配置文件如下: 

<?xml version="1.0" encoding="UTF-8" ?>  
<beans xmlns="/schema/beans"  
       xmlns:xsi="http:///2001/XMLSchema-instance"  
       xmlns:context="/schema/context"  
       xmlns:p="/schema/p"  
       xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.  
       /schema/context /schema/context/spring-context-3.">  
   <context:property-placeholder location="classpath*:conf/conf_b.properties"/>  
   <bean class=".Bean1"  
          p:driverClassName="${}"  
          p:url="${}"  
          p:username="${}"  
          p:password="${}"/>  
</beans>  

其配置文件位于类路径conf/conf_b.properties中: 

=  
=cartan  
=superman  
=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8  

问题来了 。

单独运行A模块,或单独运行B模块都是正常的,但将A和B两个模块集成后运行,Spring容器就启动不了了: 

Could not resolve placeholder '' in string value "${}"

原因是。

Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个的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模块

<?xml version="1.0" encoding="UTF-8" ?>  
<beans xmlns="/schema/beans"  
       xmlns:xsi="http:///2001/XMLSchema-instance"  
       xmlns:context="/schema/context"  
       xmlns:p="/schema/p"  
       xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.  
       /schema/context /schema/context/spring-context-3.">  
   <!--<context:property-placeholder location="classpath*:conf/conf_a.properties"/>-->  
   <bean class=".Bean1"  
          p:driverClassName="${}"  
          p:url="${}"  
          p:username="${}"  
          p:password="${}"/>  
</beans>  

B模块

<?xml version="1.0" encoding="UTF-8" ?>  
<beans xmlns="/schema/beans"  
       xmlns:xsi="http:///2001/XMLSchema-instance"  
       xmlns:context="/schema/context"  
       xmlns:p="/schema/p"  
       xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.  
       /schema/context /schema/context/spring-context-3.">  
   <!--<context:property-placeholder location="classpath*:conf/conf_b.properties"/>-->  
   <bean class=".Bean1"  
          p:driverClassName="${}"  
          p:url="${}"  
          p:username="${}"  
          p:password="${}"/>  
</beans>  

集成

<?xml version="1.0" encoding="UTF-8" ?>  
<beans xmlns="/schema/beans"  
       xmlns:xsi="http:///2001/XMLSchema-instance"  
       xmlns:context="/schema/context"  
       xmlns:p="/schema/p"  
       xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-3.  
       /schema/context /schema/context/spring-context-3.">  
   <context:property-placeholder location="classpath*:conf/conf*.properties"/>  
   <import resource=""/>  
   <import resource=""/>  
</beans>