Spring default file property values

I have a properties file outside of my war file, which is used by the system administrator to disable certain system functions. It works fine on my local machine, but when we deployed to the development environment, the properties file was not loaded and the application failed to start. I was wondering if there is a way to declare default values ​​in my ApplicationContext application for values ​​that usually come from a properties file.

I currently have this to read the properties file:

<util:properties id="myProperties" location="file:${catalina.home}/webapps/myProperties.properties"/> 

This works great until we forget to put the properties file in the right place. Is there a way to declare default values ​​or maybe read from another file if this file is not found?

thanks

+8
java spring properties configuration
source share
2 answers

Instead of <util:properties> use PropertiesFactoryBean with setIgnoreResourceNotFound=true .

For example:

 <bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="ignoreResourceNotFound"><value>true</value></property> <property name="locations"> <list> <value>classpath:default.properties</value> <value>file:${catalina.home}/webapps/myProperties.properties</value> </list> </property> </bean> 

Please note that the order of the listed files is important. Properties in later files will override previous ones.

+11
source share

As a follow-up to @ericacm's answer, if you want, you can also set default options directly in the context instead of using a separate default.properties file:

 <bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" p:location="file:${catalina.home}/webapps/myProperties.properties" p:ignoreResourceNotFound="true"> <property name="properties"> <props> <prop key="default1">value1</prop> ... </props> </property> </bean> 

Notes: p-namespace is used here for location and ignoreResourceNotFound.

+3
source share

All Articles