Aref
Aref

Reputation: 11

How to call properties file from another properties file using Spring PropertyPlaceholderConfigurer?

I'm working on a command-line Java application using Spring. I have multiple properties files stored in different locations and one properties file containing the path for all those properties. I'm using PropertyPlaceholderConfigurer, to read properties containing the locations of different properties files. I'm not sure of the best way of handling multiple properties.

The application works like this: I will pass the path for first properties file using JVM command -Dmypath=parent.properties. The properties file will look like this:

child1=/location1/child1.properties

child2=/location2/child2.properties

so on

My Parent properties configuration looks like this:

<bean id="parentProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>                
            <value>${mypath}</value>
        </list>
    </property>
</bean>

The child1 configuration looks:

<bean id="child1Property" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>                
            <value>${child1}</value>
        </list>
    </property>
</bean>

Now when I call child1 it fails to load the properties.

Upvotes: 1

Views: 2998

Answers (3)

Awanish Kumar
Awanish Kumar

Reputation: 640

I have load the parent property file first and then set the particular child1 and child2 variable in system environment variable and load from system environment variable. and its working fine.

code :

<context:property-placeholder location="file:${mypath}/*.properties" ignore-unresolvable="true" />

<bean id="systemPrereqs" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" value="#{@systemProperties}" />
        <property name="targetMethod" value="putAll" />
        <property name="arguments">
            <!-- The new Properties -->
            <util:properties>
                <prop key="LOG_LOCATION">${log.location}</prop>
                <prop key="child1">${child1}</prop>
            </util:properties>
        </property>
    </bean>

<context:property-placeholder location="file:#{systemProperties['child1']}/*.sql" ignore-unresolvable="true" />

Upvotes: 1

dntbrme
dntbrme

Reputation: 44

It may be easier to load the properties from the classpath, where the locations are included in your classpath, instead of in a file, and then the following will load all your property files.

<bean id="propertyPlaceholderConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="locations">
        <list>
            <value>classpath*:*.properties</value>
        </list>
    </property>
</bean>

Upvotes: 0

Leesrus
Leesrus

Reputation: 1115

The execution order of BeanFactoryPostProcessors such as PropertyPlaceholderConfigurer can be set by setting the property "order"(see Ordered). By setting the execution priority of parentProperty higher that that of child1Property you can ensure that parentProperty runs first, configuring the value of ${child1}.

Upvotes: 0

Related Questions