简体   繁体   中英

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

I have defined a custom PropertyPlaceholderConfigurer which makes a REST call to get the properties that are used to resolve the placeholders.

However the REST call URL is injected by Spring. And apparently this injection is called after the PropertyPlaceholderConfigurer is done. This is causing an exception, since the URL is null at the time the PlaceholderConfigurer needs it.

I need the chicken to come before the egg. Is there any way to get the injectors to run before the PlaceholderConfigurer? If not, is there some way for the PlaceholderConfigurer to get a sneak preview of the coming injections?

We run a similar configuration, in our case we have the database credentials stored in a local config file, all other properties are stored in the database.

<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>

In our case, the DatabasePropertyPlaceholderConfigurer needs to access a Spring Bean (the DataSource), so we use the BeanFactory in the override 'mergeProperties()' method to retrieve that DataSource. In your case, the configuration is a lot simpler, since you need a simple URL configuration value.

The example below might do the trick: (please notice that the two configurers use a different prefix/suffix: $[] instead of ${})

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM