I have a property configuration depending on my environment, for example:
<bean id="someConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:properties/database.${my.env}.properties</value> <value>classpath:properties/morestuff.${my.env}.properties</value> [..] </list> </property> </bean>
Now I can store different properties files in my project, for example database.prod.properties or database.qual.properties. This works fine if I run my application with -Dmy.env = foobar.
What happens if the environment is not provided? The application will not start due to a FileNotFoundException that was thrown by PropertyPlaceholderConfigurer. I do not want to keep a copy of all property files as a backup. I want to set the environment as backup if the system property is not set.
I tried to solve this problem with the second PropertyPlaceholderConfigurer, for example:
<bean id="fallbackPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:properties/default-env.properties</value> </list> </property> <property name="order" value="0" /> <property name="ignoreUnresolvablePlaceholders" value="true"/> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> </bean>
default-env.properties consists of only one property: my.env=qual .
The order is set to "0" to ensure that this bean is evaluated first. I still get the following exception:
DEBUG oscePropertySourcesPropertyResolver - Searching for key 'my.env' in [systemProperties] DEBUG oscePropertySourcesPropertyResolver - Searching for key 'my.env' in [systemEnvironment] DEBUG oscePropertySourcesPropertyResolver - Could not find key 'my.env' in any property source. Returning [null] DEBUG osbfsDefaultListableBeanFactory - Finished creating instance of bean 'someConfigurer' INFO osbfcPropertyPlaceholderConfigurer - Loading properties file from class path resource [properties/default-env.properties] INFO osbfcPropertyPlaceholderConfigurer - Loading properties file from class path resource [properties/database.${my.env}.properties] INFO osbfsDefaultListableBeanFactory - Destroying singletons in org.s pringframework.beans.factory.support.DefaultListableBeanFactory@ 435a3a: defining beans [fallbackPropertyConfigurer,someConfigurer,[..],org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor
If I comment on PropertyPlaceholderConfigurer to get rid of errors, I can use %{my.env} in other beans.
Can anyone explain this behavior?
source share