在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ [Spring] 多個(gè) PropertyPlaceholderConfigure

[Spring] 多個(gè) PropertyPlaceholderConfigurer 導(dǎo)致占位符未替換問題

這是一個(gè) Spring 項(xiàng)目,
第一個(gè) PropertyPlaceholderConfigurer ,在 context-aaa.xml 主要是配置 JDBC properties


    <bean id="jdbcPropertyPlaceholder"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="0"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="location">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>
    

其中,jdbc.properties 的內(nèi)容如下:

test.datasource.url=jdbc:mysql://192.168.0.66:3306/dev
test.datasource.username=root
test.datasource.password=123456

第二個(gè) PropertyPlaceholderConfigurer ,在 context-bbb.xml ,功能是從數(shù)據(jù)里加載 properties


    <bean id="commonsConfigurationPropertyPlaceholder"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="properties" ref="commonsConfigurationFactoryBean"/>
    </bean>

    <bean name="commonsConfigurationFactoryBean"
          class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
        <constructor-arg ref="configuration"/>
    </bean>
    
    <bean id="configuration" class="org.apache.commons.configuration.DatabaseConfiguration">
        <constructor-arg type="javax.sql.DataSource" ref="druidDataSource"/>
        <constructor-arg index="1" value="configuration"/>
        <constructor-arg index="2" value="name"/>
        <constructor-arg index="3" value="value"/>
    </bean>
                

    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url"      value="${test.datasource.url}"/>
        <property name="username" value="${test.datasource.username}"/>
        <property name="password" value="${test.datasource.password}"/>
    </bean>

問題來了,加載 DruidDataSource 時(shí),占位符并沒有被替換。

我已經(jīng)加了 PropertyPlaceholderConfigurer 的 order 屬性排序,以及 ignoreUnresolvablePlaceholders 為 true 了,但是問題依舊。

DruidDataSource 作為第二個(gè) PropertyPlaceholderConfigurer 的依賴,它的配置占位符不會(huì)被處理嗎?
我斷點(diǎn)發(fā)現(xiàn),BeanFactoryPostProcessor 的處理,在程序的報(bào)錯(cuò)之后才執(zhí)行,是怎么回事?

https://jira.spring.io/browse...

我找到了解決方案:
The alternative is to

(a) use the new PropertySourcesPlaceholderConfigurer instead of the traditional PropertyPlaceholderConfigurer;

(b) eliminate the first PropertySourcesPlaceholderConfigurer ;

(c) register a PropertySource with the ApplicationContext's Environment that contains the properties for the placeholders that need replacement in the PropertySourcesPlaceholderConfigurer

我想問第三步怎么操作?。?/p>

回答
編輯回答
哚蕾咪

如果我沒有理解錯(cuò),你的執(zhí)行邏輯是:

DruidDataSource屬性 依賴 -> jdbc.properties
其他beans屬性 依賴 -> DruidDataSource

那么你需要做的是在spring創(chuàng)建beans后

做后處理之前為DruidDataSource設(shè)置屬性

做后處理之后為其他beans創(chuàng)建PropertyPlaceholder

如下方式可供參考(代碼需要根據(jù)你的情況修改后才能用):

package com.bixuebihui;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;


public class BeanPostPrcessorImpl implements BeanPostProcessor {
    private Properties properties="jdbc.properties";
    
    // Bean 實(shí)例化之前進(jìn)行的處理
    public Object postProcessBeforeInitialization(Object bean, String beanName)
           throws BeansException {
       //System.out.println("對(duì)象" + beanName + "開始實(shí)例化");
        if(beanName.equals("druidDataSource")){
            try {
                //set druidDataSource's properties use properties
                ((DruidDataSource)bean).setUrl(properties.getProperty("url"));
                ......
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
       return bean;
    }

    // Bean 實(shí)例化之后進(jìn)行的處理
    public Object postProcessAfterInitialization(Object bean, String beanName)
           throws BeansException {
       //System.out.println("對(duì)象" + beanName + "實(shí)例化完成");
        //<bean class="com.spring.test.di.BeanPostPrcessorImpl"/>
       return bean;
    }
    
    public String getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}
    <bean id="properties"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
        <value>classpath:jdbc.properties</value>
        </list>
    </property>
    </bean>

    <bean class="com.bixuebihui.BeanPostPrcessorImpl">
        <property name="properties" value="properties" />
    </bean>

參考:
https://segmentfault.com/a/11...

2017年10月4日 15:35
編輯回答
傲嬌范

檢查一下是否是加了 PropertyPlaceholderConfigurer 的 order 屬性排序,以及 ignoreUnresolvablePlaceholders 為 true 后xml文件沒有生效的原因。

2017年10月7日 14:31