I have a property configuration depending on my environment, like this:
<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 keep different property files in my project, for example database.prod.properties or database.qual.properties. This works fine, if I start my application with -Dmy.env=foobar.
What happens if no environment is supplied? The application won't start, because of a FileNotFoundException thrown by the PropertyPlaceholderConfigurer. I don't want to keep a copy of all property files as a fallback. What I want is to set a environment as fallback, if the system property is not set.
I tried to solve this with a second PropertyPlaceholderConfigurer like:
<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 just one property: my.env=qual
.
The order is set to '0' to be sure, that this bean is evaluated first. I still get the following exception:
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'my.env' in [systemProperties]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'my.env' in [systemEnvironment]
DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'my.env' in any property source. Returning [null]
DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'someConfigurer'
INFO o.s.b.f.c.PropertyPlaceholderConfigurer - Loading properties file from class path resource [properties/default-env.properties]
INFO o.s.b.f.c.PropertyPlaceholderConfigurer - Loading properties file from class path resource [properties/database.${my.env}.properties]
INFO o.s.b.f.s.DefaultListableBeanF开发者_开发问答actory - Destroying singletons in org.springframework.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 out the PropertyPlaceholderConfigurer to get rid of the errors I can use %{my.env}
in other beans.
Can anybody explain this behavior?
You can set your default value (in Spring 3) like this:
${my.env:qual}
I suggest to localize your build using the Maven Assembly plugin instead of fiddling with filenames in your Spring context.
A longshoot try to add
property name="ignoreUnresolvablePlaceholders" value="true"/>
to your first bean as well
as farmor said, the property 'ignoreUnresolvablePlaceholders' should be set with true for the 1st bean too, as explained in API..
Default is "false": An exception will be thrown if a placeholder fails to resolve. Switch this flag to "true" in order to preserve the placeholder String as-is in such a case, leaving it up to other placeholder configurers to resolve it.
you can also use:
<property name="ignoreResourceNotFound" value="true"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
精彩评论