zoukankan      html  css  js  c++  java
  • Spring配置:用context:property-placeholder替换PropertyPlaceholderConfigurer

    1、有时候需要从properties文件中加载配置,以前的方式是这样的:

    [html] view plain copy
    1. <bean id="jdbcProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    2.     <property name="locations">  
    3.         <list>  
    4.             <value>classpath*:/spring/jdbc.properties</value>  
    5.         </list>  
    6.     </property>  
    7. </bean>  

    spring推荐使用如下方式配置:

    [html] view plain copy
    1. <context:property-placeholder location="classpath:spring/jdbc.properties" />  

    以上两种方式,在bean定义中依然可以通过“${}”这种方式来去值:

    [html] view plain copy
    1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
    2.         <property name="driverClassName" value="${jdbc.driverClassName}" />  
    3.         <property name="url" value="${jdbc.url}" />  
    4.         <property name="username" value="${jdbc.username}" />  
    5.         <property name="password" value="${jdbc.password}" />  
    6.     </bean>  

    注:<context:property-placeholder/>这个基于命名空间的配置,其实内部就是创建一个PropertyPlaceholderConfigurer Bean而已。

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

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

    [html] view plain copy
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.        xmlns:context="http://www.springframework.org/schema/context"  
    5.        xmlns:p="http://www.springframework.org/schema/p"  
    6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
    7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
    8.    <context:property-placeholder location="classpath*:conf/conf_a.properties"/>  
    9.    <bean class="com.xxx.aaa.Bean1"  
    10.           p:driverClassName="${modulea.jdbc.driverClassName}"  
    11.           p:url="${modulea.jdbc.url}"  
    12.           p:username="${modulea.jdbc.username}"  
    13.           p:password="${modulea.jdbc.password}"/>  
    14. </beans>  

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

    [html] view plain copy
    1. modulea.jdbc.driverClassName=com.mysql.jdbc.Driver  
    2. modulea.jdbc.username=cartan  
    3. modulea.jdbc.password=superman  
    4. modulea.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8  


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

    [html] view plain copy
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.        xmlns:context="http://www.springframework.org/schema/context"  
    5.        xmlns:p="http://www.springframework.org/schema/p"  
    6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
    7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
    8.    <context:property-placeholder location="classpath*:conf/conf_b.properties"/>  
    9.    <bean class="com.xxx.bbb.Bean1"  
    10.           p:driverClassName="${moduleb.jdbc.driverClassName}"  
    11.           p:url="${moduleb.jdbc.url}"  
    12.           p:username="${moduleb.jdbc.username}"  
    13.           p:password="${moduleb.jdbc.password}"/>  
    14. </beans>  

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

    [html] view plain copy
    1. moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver  
    2. moduleb.jdbc.username=cartan  
    3. moduleb.jdbc.password=superman  
    4. 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:

    [html] view plain copy
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.        xmlns:context="http://www.springframework.org/schema/context"  
    5.        xmlns:p="http://www.springframework.org/schema/p"  
    6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
    7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
    8.    <!--<context:property-placeholder location="classpath*:conf/conf_a.properties"/>-->  
    9.    <bean class="com.xxx.aaa.Bean1"  
    10.           p:driverClassName="${modulea.jdbc.driverClassName}"  
    11.           p:url="${modulea.jdbc.url}"  
    12.           p:username="${modulea.jdbc.username}"  
    13.           p:password="${modulea.jdbc.password}"/>  
    14. </beans>  

    B模块b.xml: 

    [html] view plain copy
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.        xmlns:context="http://www.springframework.org/schema/context"  
    5.        xmlns:p="http://www.springframework.org/schema/p"  
    6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
    7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
    8.    <!--<context:property-placeholder location="classpath*:conf/conf_b.properties"/>-->  
    9.    <bean class="com.xxx.bbb.Bean1"  
    10.           p:driverClassName="${moduleb.jdbc.driverClassName}"  
    11.           p:url="${moduleb.jdbc.url}"  
    12.           p:username="${moduleb.jdbc.username}"  
    13.           p:password="${moduleb.jdbc.password}"/>  
    14. </beans>  


    集成: 

    [html] view plain copy
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.        xmlns:context="http://www.springframework.org/schema/context"  
    5.        xmlns:p="http://www.springframework.org/schema/p"  
    6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
    7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  
    8.    <context:property-placeholder location="classpath*:conf/conf*.properties"/>  
    9.    <import resource="a.xml"/>  
    10.    <import resource="b.xml"/>  
    11. </beans>  


    Spring为什么要这样呢?细想想是有道理的,一个项目或一个系统的配置应该放在一起,不宜分散。 
    这样才可以做到统一管控,否则到处都有配置,到底是加载哪个配置文件呢?有时你还会不小心让JAR中的Spring配置文件加载一个位于JAR中的属性文件,而外面有更改不了。如果Spring使用了这种机制,即使JAR包中的Spring配置文件使用<context:property-placeholder/>引用到JAR中的属性文件,只要你要外而的Spring配置文件中显示提供一个<context:property-placeholder/>指定另一个属性文件 ,就可以覆盖JAR中的默认配置了。 

  • 相关阅读:
    状态模式
    策略模式Strategy(对象行为型)
    模板方法模式
    Java_观察者模式(Observable和Observer) -转
    Cordova自定义插件
    MongoVUE查询备忘
    C#关于HttpClient的统一配置(一)
    C#邮件收发
    WebApi统一输出接口
    移动开发兼容性问题及性能优化
  • 原文地址:https://www.cnblogs.com/moxiaotao/p/9345825.html
Copyright © 2011-2022 走看看