• Spring的PropertyPlaceholderConfigurer强制使用默认值的坑


    1.问题

    dubbo client配置:

    	<dubbo:reference id="channelCustomerClient" interface="com.gttown.crm.channel.service.ChannelCustomerService"
        timeout="60000" check="false" filter="clientFilter" retries="0" validation="false"
        url="${dubbo.url.channel:#{null}}"/>
    

    dubbo.properties:

    		zipkin.url=http://zipkin.dev.great-tao.com
    		dubbo.application=gttown-user-web
    		dubbo.register=false
    		dubbo.port=20880
    		dubbo.url.channel=dubbo://127.0.0.1:20881
    

    dubbo配置时,预期效果:url="${dubbo.url.channel:#{null}}" 会先读取配置文件dubbo.url.channel的值如果有值则读取,若配置文件无该值则用默认值null。
    但是事实上无论dubbo.properties配置文件是否有dubbo.url.channel。url的值都会强制使用默认值null。

    PropertyPlaceholderConfigurer配置参考。 (具体参考http://elim.iteye.com/blog/2387138)

    2.原因

    spring.xml 里的PropertyPlaceholderConfigurer配置:

    	<bean id="placeholderConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    	        <property name="locations">
    	            <list>
    	                <value>classpath*:/*.properties</value>
    	            </list>
    	        </property>
    	        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    	    </bean>
    

    db-user.xml 里的PropertyPlaceholderConfigurer配置:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="order" value="-1"/>
            <property name="locations">
                <list>
                    <value>classpath:db.properties</value>
                </list>
            </property>
            <property name="ignoreUnresolvablePlaceholders" value="true"/>
        </bean>
    

    spring.xml的PlaceholderConfigurer配置扫描了classpath下的全部文件,但是加载顺序order使用的是默认值。
    db-user.xml的PlaceholderConfigurer配置只扫描了classpath下的db.properties,加载顺序order为-1最后加载。

    url="${dubbo.url.channel:#{null}}"配置时由于db-user.xml的PlaceholderConfigurer后加载,而该PlaceholderConfigurer只扫描了db.properties。该文件没有dubbo.url.channel的值,导致url使用默认值null。

    3.解决

    • 方案1:
      spring.xml 里的PropertyPlaceholderConfigurer配置加上

            <property name="order" value="-1"/>
      

    建议:扫面范围越大的PropertyPlaceholderConfigurer越后面加载。

    • 方案2:
      删除db-user.xml 以及以其他配置文件里的精确扫描指定文件的PropertyPlaceholderConfigurer配置。

    • 方案3:
      spring.xml 里的PropertyPlaceholderConfigurer配置,修改分隔符(默认分隔符为:)

        <property name="valueSeparator" value="&"/>
      

    使用& 作为分隔符。

    dubbo-clien.xml配置修改为
    url="${dubbo.url.channel&#{null}}

  • 相关阅读:
    对象并不一定都是在堆上分配内存的
    Minor GC、Major GC和Full GC之间的区别
    JAVA内存分配与回收策略
    HotSpot虚拟机对象相关内容
    JAVA运行时数据区域
    Java 的强引用、弱引用、软引用、虚引用
    去哪儿网支付系统架构演进(下篇)
    去哪儿网支付系统架构演进(上)
    心智模式:如何改善我们的心智模式?
    心智模式:心智模式成熟的标志
  • 原文地址:https://www.cnblogs.com/huanghuizhou/p/9437090.html
Copyright © 2020-2023  润新知