1

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.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132

1 Answers1

1

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.

Ken Bekov
  • 13,696
  • 3
  • 36
  • 44