问题
We access a java property in spring like this:
<property name="fileSizeLimit" value="${someProperty}" />
The bean declares
int fileSizeLimit = 9999;
How can I set a default if "someProperty" is missing in the properties-file?
ATM, we get NumberFormatException because spring calls the int-setter with the name of the property "someProperty". When the property is set, everything works fine.
http://static.springsource.org/spring/docs/1.1.5/api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html says:
Default property values can be defined via "properties", to make overriding definitions in properties files optional. A configurer will also check against system properties (e.g. "user.dir") if it cannot resolve a placeholder with any of the specified properties. This can be customized via "systemPropertiesMode".
Does this set the order in which properties are looked up? Where do I configure this?
TIA, Bastl.
回答1:
For your first question, you can set a default value for placeholder using the following syntax, where 9999 is the default.
<property name="fileSizeLimit" value="${someProperty:9999}" />
For your second question, the property systemPropertiesModeName determines the order in which properties are resolved, properties file versus system properties. For example,
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
Tells the PropertyPlaceholderConfigurer to look at the system property before the properties file.
The values for systemPropertiesModeName are:
SYSTEM_PROPERTIES_MODE_FALLBACK (default)
Check system properties if not resolvable in the specified properties.
SYSTEM_PROPERTIES_MODE_NEVER
Never check system properties.
SYSTEM_PROPERTIES_MODE_OVERRIDE
Check system properties first, before trying the specified properties.
I usually use SYSTEM_PROPERTIES_MODE_OVERRIDE and default values in my placeholders, so the order would be
- System property
- Properties file
- Placeholder default
回答2:
You could define default values for PropertyPlaceHolder
in bean definition in xml file.
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
id="corePlaceHolder">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="systemPropertiesModeName"
value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="searchSystemEnvironment" value="true"/>
<property name="locations">
<list>
<value>classpath*:config/*/......./*.properties</value>
</list>
</property>
<property name="properties">
<props>
<prop key="fileSizeLimit">123</prop>
</props>
</property>
</bean>
See http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html for further details
回答3:
In Spring 3 you can do ${someProperty:defaultValue}
. I really hope you're not using 1.1.5.
回答4:
When using the PropertyPlaceholderConfigurer, according to its Javadoc, "the placeholder properties file is supposed to contain an entry for each defined placeholder".
You can, however, provide multiple locations (properties files) for your PropertyPlaceholderConfigurer, and use one of them as defaults. That way you can ensure you always have the default values you need.
If you want your application to throw an exception when an undefined property is used, ensure that "ignoreUnresolvablePlaceholders" is set to false in your PropertyPlaceholderConfigurer.
来源:https://stackoverflow.com/questions/8214862/propertyplaceholder-in-spring