论坛首页 Java企业应用论坛

关于<context:property-placeholder>的一个有趣现象

浏览 43526 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-08-16   最后修改:2013-08-16
先来看下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模块

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这样做是利大于弊的。
   发表时间:2013-08-17  
生成的配置类默认是单例的,所以才会出现冲突。不过这个例子很好,至少没思考过。
0 请登录后投票
   发表时间:2013-08-17  
我看了下你的回答,我也提点:

1、在PropertyPlaceholderBeanDefinitionParser的父类中shouldGenerateId返回true,即默认会为每一个bean生成一个唯一的名字; 如果使用了两个<context:property-placeholder则注册了两个PropertySourcesPlaceholderConfigurer Bean;所以不是覆盖(而且bean如果同名是后边的bean定义覆盖前边的);

2、PropertySourcesPlaceholderConfigurer本质是一个BeanFactoryPostProcessor,spring实施时如果发现这个bean实现了Ordered,则按照顺序执行;默认无序;

3、此时如果给<context:property-placeholder加order属性,则会反应出顺序,值越小优先级越高即越早执行;
比如
   <context:property-placeholder order="2" location="classpath*:conf/conf_a.properties"/> 
   <context:property-placeholder order="1" location="classpath*:conf/conf_b.properties"/>

此时会先扫描order='1' 的,如果没有扫描order='2'的


4、默认情况下ignore-unresolvable;即如果没找到的情况是否抛出异常。默认false:即抛出异常;
<context:property-placeholder location="classpath*:conf/conf_a.properties" ignore-unresolvable="false"/>

所以你的测试会发生如下问题:
只有一个成功(即使指定order);

所以可以这样处理
<context:property-placeholder order="1" location="classpath*:conf/conf_a.properties" ignore-unresolvable="true"/>

<context:property-placeholder order="2" location="classpath*:conf/conf_b.properties" ignore-unresolvable="false"/>


即指定顺序,且只有最后一个ignore-unresolvable=false;

但这样好麻烦啊。。


我打开iteye目前有个问题,打开几个页面后,接着就无响应了;等会有可以访问了,大家有这个问题吗?
0 请登录后投票
   发表时间:2013-08-17  
也可以自己手动配置一个或者多个这种bean,每一个可以加载多个prperties文件

locations属性,还能替换表达式前缀后缀等
0 请登录后投票
   发表时间:2013-08-19  
说到底的原因是,虽然你分了模块,但是spring容器只有一个;所以分模块(分配置文件)在spring当中更多的作用是分系统层次,不是真正意义上的分平行的模块。
0 请登录后投票
   发表时间:2013-08-23  
stamen 写道
换句话说,即Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉(其实Spring如果提供一个警告就好了)

这个说法貌似不对,我看过楼主的书,也简单读过spring源码,PropertyPlaceholderConfigurer本质是一个BeanFactoryPostProcessor,容器中当然可以允许定义多个,我的项目就用到了多个。invokeBeanFactoryPostProcessors方法里面会按order顺序invoke每个BeanFactoryPostProcessor,出现错误的原因是因为A配置器在执行的时候,只加载了A的properties,但是此时会扫描整个容器中的bean,发现有moduleb的占位符,无法解析。
所以加上配置,忽略无法解析的占位符就行<property name="ignoreUnresolvablePlaceholders" value="true"/>,这样A配置器忽略掉的占位符等到B配置器执行的时候就解析成功了。当然这样无法在启动时及时发现个别配置错误的问题

所以tao哥说的有道理!
0 请登录后投票
   发表时间:2013-11-11  
我现在有个问题,场景比较复杂,简单说就是这边也有两个properties需要引入,但一个在classpath下,一个在web-inf下,那该怎么配置啊?
0 请登录后投票
   发表时间:2013-11-11  
blue_ysy 写道
我现在有个问题,场景比较复杂,简单说就是这边也有两个properties需要引入,但一个在classpath下,一个在web-inf下,那该怎么配置啊?

如果是使用的ContextLoaderListener加载的 可以这样配

<context:property-placeholder location="classpath:resources.properties"/>
<context:property-placeholder location="/WEB-INF/resources.properties"/>

不写classpath前缀 在web环境中使用的ServletContext相对路径加载的
0 请登录后投票
   发表时间:2013-11-11  
jinnianshilongnian 写道
blue_ysy 写道
我现在有个问题,场景比较复杂,简单说就是这边也有两个properties需要引入,但一个在classpath下,一个在web-inf下,那该怎么配置啊?

如果是使用的ContextLoaderListener加载的 可以这样配

<context:property-placeholder location="classpath:resources.properties"/>
<context:property-placeholder location="/WEB-INF/resources.properties"/>

不写classpath前缀 在web环境中使用的ServletContext相对路径加载的


这不就是楼主说的问题嘛,这样配置会报错的
0 请登录后投票
   发表时间:2013-11-11  
3.2 中用 @PropertySource 和 Environment 吧。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics