Defaults for Property Values? I didn’t know that?
I 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)
Pingback: Tweets that mention Technophile Blog ยป Using Default Values for Properties in Spring -- Topsy.com
Pingback: Spring Default Values – Lessons Learned « Bitlingo
Pingback: Spring Default Values – Lessons Learned
Very useful !!!
Thanks
Tarun
very useful