簡體   English   中英

我怎樣才能讓Spring運行我的自定義PropertyPlaceholderConfigurer _after_注入器運行

[英]How can I get Spring to run my custom PropertyPlaceholderConfigurer _after_ the injectors run

我定義了一個自定義的PropertyPlaceholderConfigurer,它進行REST調用以獲取用於解析占位符的屬性。

但是,REST調用URL是由Spring注入的。 顯然在完成PropertyPlaceholderConfigurer 之后將調用此注入。 這會導致異常,因為在PlaceholderConfigurer需要URL時該URL為null。

我需要雞肉先於雞蛋。 有什么方法可以使PlaceholderConfigurer 之前的噴射器運行? 如果沒有,PlaceholderConfigurer是否可以通過某種方式來預覽即將到來的注射?

我們運行類似的配置,在這種情況下,我們將數據庫憑據存儲在本地配置文件中,所有其他屬性都存儲在數據庫中。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="1"/>
    <property name="locations">
        <list>
            <value>classpath:app.properties</value>
        </list>
    </property>
    <property name="placeholderPrefix" value="$["/>
    <property name="placeholderSuffix" value="]"/>
</bean>

<bean id="propertyConfigurer" class="com.acme.util.DatabasePropertyPlaceholderConfigurer">
    <property name="order" value="2"/>
    <property name="dataSourceName" value="dataSource"/>
</bean>

在我們的例子中,DatabasePropertyPlaceholderConfigurer需要訪問一個Spring Bean(數據源),因此我們在重寫'mergeProperties()'方法中使用BeanFactory來檢索該數據源。 就您而言,配置要簡單得多,因為您需要一個簡單的URL配置值。

下面的示例可以解決這個問題:(請注意,兩個配置器使用不同的前綴/后綴:$ []代替$ {})

<bean id="propertyConfigurer" class="com.acme.util.RESTPropertyPlaceholderConfigurer">
    <property name="order" value="2"/>
    <property name="url" value="$[config.url]"/>
</bean>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM