Using Default Values for Properties in Spring
Join the DZone community and get the full member experience.
Join For FreeI was looking through the Spring Greenhouse application and discovered an existing feature that I was unaware of. We can set a default value when configuring PropertyPlaceholderConfigurer.
1. Set the default value separator in config
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:application.properties" />
<property name="ignoreResourceNotFound" value="true" /> <-- Ignore file not found -->
<property name="ignoreUnresolvablePlaceholders" value="true" /> <-- Ignore when tokens are not found -->
<!-- Token that separates default values on a placeholder-by-placeholder basis e.g. ${server.name?localhost} -->
<property name="valueSeparator" value="?" />
...
</bean>
2. Set the default values for your properties
<bean id="myServer" class="com.gordondickens.myapp.MyServerConfig">
<property name="serverName" value="${server.name?localhost}" />
<property name="serverPort" value="${server.port?25}" />
...
</bean>
Notes
- If unspecified, the default separator is a colon “:”
- This option is not available when using the “context” namespace – (submitted Jira ticket: SPR-7794)
From http://gordondickens.com/wordpress/2010/12/06/using-default-values-for-properties-in-spring/
Opinions expressed by DZone contributors are their own.
Comments