bea...">

PropertyPlaceHolder in spring

We access the java property in spring as follows:

<property name="fileSizeLimit" value="${someProperty}" /> 

bean announces

 int fileSizeLimit = 9999; 

How to set default value if someProperty property is missing in the properties file?

ATM, we get a NumberFormatException because spring throws an int-setter with the property name 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 specified through "properties" to optionally override definitions in property files. The configurator will also check the properties of the system (for example, "user.dir") if it cannot resolve the placeholder with any of the specified properties. This can be configured using "systemPropertiesMode".

Does this order describe the order in which properties are searched? Where can I customize this?

TIA, Bastl.

+1
spring properties
source share
4 answers

For your first question, you can set the default value for the placeholder using the following syntax, where the default value is 9999.

 <property name="fileSizeLimit" value="${someProperty:9999}" /> 

For your second question, the systemPropertiesModeName property determines the order of resolving properties, file properties, and system properties. For example,

 <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> 

Tells PropertyPlaceholderConfigurer to view the system property before the property file.

Values ​​for systemPropertiesModeName:

 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 the default values ​​in my placeholders, so the order will be

  • System property
  • Properties file
  • Default placeholder
+11
source share

In Spring 3, you can do ${someProperty:defaultValue} . I really hope you are not using 1.1.5.

+2
source share

You can define default values ​​for PropertyPlaceHolder in the definition of bean in the 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 details

+2
source share

When using the PropertyPlaceholderConfigurer, according to its Javadoc , "the placeholder property file must contain an entry for each defined placeholder."

However, you can provide several places (property files) for your PropertyPlaceholderConfigurer and use one of them by default. In this way, you can ensure that you always have the default values ​​that you need.

If you want your application to throw an exception when using the undefined property, make sure ignoreUnresolvablePlaceholders is set to false in your PropertyPlaceholderConfigurer.

+1
source share

All Articles