Spring Default Values – Lessons Learned

Spring uses a class it called PropertyPlaceholderConfigurer in order to provide property values within the Spring Context File. There are ways to add default values incase the property key does not exist. For instance, in the following scenario I have two bean ids and I want to use property substitution to determine which bean to inject into another bean:

Spring Context File

1
2
3
<bean id="addressSearchDao"class="com.foo.AddressSearchDaoImpl"/>
<bean id="addressDaoMock"class="com.foo.MockAddressDaoImpl"/>
<bean id="addressSearchService"class="com.foo.AddressSearchServiceImpl" p:baseDao-ref="addressSearch${addressSearchService.dao:Dao}"/>

Properties File

1
addressSearchService.dao=DaoMock

The reason for me posting this is because I could not find clear documentation for the default values and so I was incorrectly doing the property default values. The separator is very important because you can set it to be any character you want, but by default it is : (colon).

Here are the resources I found including my post that refers to the handling of default property values.

RESOURCES

http://stackoverflow.com/questions/5410017/property-placeholder-for-imports-bean-refs

https://jira.springsource.org/browse/SPR-4785

http://java.dzone.com/articles/using-default-values

http://gordondickens.com/wordpress/2010/12/06/using-default-values-for-properties-in-spring/

Share and Enjoy