Spring踩坑记录

1. Spring properties配置项不能解析问题

本地部分配置文件迁到disconf,希望disconf的配置文件交由spring托管。这样的话,原有代码中引用配置的地方就不用变(还是用${key}的方式)。

在disconf上找到了配置方式:http://disconf.readthedocs.io/zh_CN/latest/tutorial-client/src/Tutorial5.html

结果启动后报异常,解析不了配置:Could not resolve placeholder

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'zookeeper.registry.address' in string value "${zookeeper.registry.address}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:209)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141)
    at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82)
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:208)
    ... 37 more
View Code

查看disconf的配置,断点,看到disconf交由spring托管的配置已经加载。
后面找到原因,是应用配置了多个 org.springframework.context.support.PropertySourcesPlaceholderConfigurer,导致了配置覆盖。
https://www.cnblogs.com/YingYue/p/5699962.html

保留的配置:

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

放disconf的配置:

<!-- 使用托管方式的disconf配置(无代码侵入, 配置更改不会自动reload) -->
<bean id="configproperties_no_reloadable_disconf"
    class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>config.properties</value>
        </list>
    </property>
</bean>

<bean id="propertyConfigurerForProject1"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="propertiesArray">
        <list>
            <ref bean="configproperties_no_reloadable_disconf" />
        </list>
    </property>
</bean>
View Code

 

转载于:https://www.cnblogs.com/kevin-yuan/p/7867224.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值