Spring中PropertyPlaceholderConfigurer替换占位符的问题

最近在做项目的时候,碰到了一个问题,纠结了好久,现在记录一下

问题

多个Maven项目聚合的时候,每个maven都有自己的配置文件,并且都用了PropertyPlaceholderConfigurer替换占位符,然后启动的时候一直报错,说替换失败;问题症结就是 spirng配置多个PropertyPlaceholderConfigurer的问题

原因

在spring bean装配时,一个PropertyPlaceholderConfigurer就是一个后置处理器BeanFactoryPostProcessor。在装配完PropertyPlaceholderConfigurer之后,就会触发org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(Collection

/**
 * Invoke the given BeanFactoryPostProcessor beans.
 */
private void invokeBeanFactoryPostProcessors(
        Collection<? extends BeanFactoryPostProcessor> postProcessors, ConfigurableListableBeanFactory beanFactory) {

    for (BeanFactoryPostProcessor postProcessor : postProcessors) {
        postProcessor.postProcessBeanFactory(beanFactory);
    }
}

假设一下,你配置了两个PropertyPlaceholderConfigurer实例
A模板的jdbc.xml配置文件

<bean id="propertyConfigurer" class="com.zheng.common.plugin.EncryptPropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>

B模板的shiro.xml配置文件

    <context:property-placeholder location="classpath*:zheng-upms-client.properties"/>

然后A模板中的jdbc.properties 和 B中的zheng-upms-client.properties 文件都在A模板中;
A依赖了B;启动A项目,IOC会先实例化这两个配置的PropertyPlaceholderConfigurer;
假如先实例化了A中的PropertyPlaceholderConfigurer实例,那么它会去替换所有被标记为 ${} 的占位符,这个时候替换到B模板中的一些占位符之后,肯定就会报错了,因为B模板中的占位符是在 zheng-upms-client.properties这个属性文件中;

解决方案

一、使用一个PropertyPlaceholderConfigurer实例加载

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <value>classpath:redis.properties</value>
                 <value>classpath:zheng-upms-client.properties</value>
            </list>
        </property>
    </bean>

但是这样解决真的是超级没有诚意了,本来就是解决不通模块之间的问题啊

二、配置加载顺序,并设置替换失败不报错

1.让B中的实例配置order=1 先加载,并且设置ignore-unresolvable=”true”表示替换失败不报错

    <context:property-placeholder order="1" ignore-unresolvable="true" location="classpath*:zheng-upms-client.properties"/>

2.设置A中,order=2 表示后加载,但是不设置ignore-unresolvable属性,因为最后还是要检查是否有剩余未替换的属性


    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="2" />
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>

总结

思路就是 当有多个实例的时候,让他们一个一个的去替换,替换失败不提示错误,等做后一个实例替换的时候如果还有没有被替换的就提示错误!
所以要设置 order 来排序,因为必须让最后一个加载的去检查替换错误,之前的都可以不用检查

猜你喜欢

转载自blog.csdn.net/u010634066/article/details/80794631
今日推荐