jrey
jrey

Reputation: 2183

Spring - how to use PropertyPlaceholderConfigurer to dynamically load files

i have the next properties files with Spring Framework

config.properties
with content

environment=devel //posible values: devel, testing, prod

and with the previous environment property, choose some of the following files to load dynamically

config-service1-devel.properties
config-service1-testing.properties
config-service1-prod.properties
config-serviceN-devel.properties
config-serviceN-testing.properties
config-serviceN-prod.properties

and then, with spring i want load the properties, i'm solve to load the first properties file but i dont understand how to use expression language to complete the values of the dependent properties.

<bean id="MainApplicationProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location"
        value="file://#{systemProperties['jboss.server.home.dir']}/conf/services.properties" />

    <property name="placeholderPrefix" value="$mainProperty{" />
    <property name="placeholderSuffix" value="}" />
</bean>
<bean id="SecondApplicationProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    depends-on="MainApplicationProperties">

    <property name="locations">
        <list>
            <value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceOne/service1-$mainProperty{environment}.properties</value>
            <value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceTwo/service2-$mainProperty{environment}.properties</value>
            <value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceN/serviceN-$mainProperty{environment}.properties</value>
        </list>
    </property>

</bean>

the error output is the next,

java.io.FileNotFoundException: /..../conf/serviceOne/service1-$mainProperty{environment}.properties (No such file or directory)

my opinion is, the value has not replaced

helpme, thanks

Upvotes: 0

Views: 1247

Answers (1)

Pavel Horal
Pavel Horal

Reputation: 18224

The problem is that when BeanFactoryPostProcessors are starting to be invoked, they are already instantiated. So even thou the first PropertyPlaceholderConfigurer modifies the bean definition of the second PropertyPlaceholderConfigurer, it has no effect as both beans have been already instantiated.

Upvotes: 1

Related Questions