// shall I write some keywords here to boost search engine ranking?

Tuesday, April 29, 2008

Spring PropertyPlaceholderConfigurer with default value

I like to use Spring framework's PropertyPlaceholderConfigurer to link to a .properties file to access my application configuration.

However, if a properties entry is missing from properties file, you will get BeanDefinitionStoreException. This is reasonable in most of time. But sometime you will just want to set some default value for certain entry (i.e. new entry that only applicable for new version and you want it to be backward compatible with old version configuration file).

For those scenarios, default value is supported by PropertyPlaceholderConfigurer as well by its setProperties(Properties props) method. Below is how it will look like:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>my_config.properties</value></property>
<property name="properties">
<props>
<prop key="entry.1">123</prop>
</props>
</property>
</bean>

4 comments:

Anonymous said...

thank you! Really helpful information!

Nick Wiedenbrueck said...

Thanks. Very useful.

Durgesh said...

Add in


This will override local property definitions if they are found in external property file.

Durgesh said...

Add in


This will override local property definitions if they are found in external property file.