Miguel Lopez
Miguel Lopez

Reputation: 78

Loading multiple external properties files from spring

I want to load all the properties files with Spring from an external folder. I successfully load one file but adding a wildcard to the mix does not seem to work.

This works (load test.properties):

<bean id="propertiesLocation"
    class="org.springframework.web.context.support.ServletContextParameterFactoryBean">
    <property name="initParamName"><value>file://EXTERNAL_DIRECTORY/test.properties</value></property>
</bean>

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" ref="propertiesLocation"></property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="0"/>
</bean>

This does not (load *.properties):

<bean id="propertiesLocation"
    class="org.springframework.web.context.support.ServletContextParameterFactoryBean">
    <property name="initParamName"><value>file://EXTERNAL_DIRECTORY/*.properties</value></property>
</bean>

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" ref="propertiesLocation"></property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="0"/>
</bean>

The error:

Caused by: java.io.FileNotFoundException: /EXTERNAL_DIRECTORY/*.properties (No es un directorio)

How can I make Spring load all the external properties files in a folder?

Edit: I use the first bean (ServletContextParameterFactoryBean) because in the project I retrieve the path from the web.xml file. I forgot about this and just pasted the path in the bean, it is incorrect but has nothing to do with the question.

Upvotes: 1

Views: 1743

Answers (1)

Ken Bekov
Ken Bekov

Reputation: 14015

Try use following:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="file://EXTERNAL_DIRECTORY/*.properties"/>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="0"/>
</bean>

If you need include more resources you can do next:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" >
        <list>
            <value>classpath:single.properties"</value>
            <value>file://EXTERNAL_DIRECTORY/*.properties"</value>
            <value>file://ANOTHER_EXTERNAL_DIRECTORY/*.properties"</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="0"/>
</bean>

With default implementation of PropertyEditor, Spring will convert strings into Resource. You can find details in documentation.

Hope this will be helpful.

Upvotes: 1

Related Questions