Using properties in spring config

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#0]; root of factory hierarchy Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [properties/database.${my.env}.properties] cannot be opened because it does not exist at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:87) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) [..] 

If I comment on PropertyPlaceholderConfigurer to get rid of errors, I can use %{my.env} in other beans.

Can anyone explain this behavior?

+4
source share
5 answers

You can set the default value (in Spring 3) as follows:

 ${my.env:qual} 
+4
source

I suggest localizing your assembly using the Maven Assembly plugin instead of messing with the file names in your Spring context.

+1
source

Long arrow trying to add

property name = "ignoreUnresolvablePlaceholders" value = "true" / ">

to your first bean as well

0
source

as the farmer said, the ignoreUnresolvablePlaceholders' property must be set to true for the 1st bean, as described in the API ..

Default is false: an exception will be thrown if the placeholder does not work. Allow. Set this flag to "true" to save the placeholder String as-is in that case, leaving it to another to change it.

0
source

you can also use:

 <property name="ignoreResourceNotFound" value="true"/> <property name="ignoreUnresolvablePlaceholders" value="true"/> 
0
source

Source: https://habr.com/ru/post/1369627/


All Articles