Friday, April 06, 2012

Inject constants in bean fields with Spring 3.0

Today, I imported some properties in my Spring config file. I wrote something like this :

<!-- define properties from a file -->
<util:properties id="securityProperties"  location="classpath:/securityProperties.properties" />

I could also wrote :

<!-- look for properties from a JNDI context -->
<jee:jndi-lookup id="securityProperties" jndi-name="props/security" />

... or even more simpler :

<!-- simply declare properties directly -->
<util:properties id="securityConstant">
    <prop key="cas.base.url">blablabla</prop>
</util:properties>

So, I needed to retrieve my cas.base.url. In my bean, I just had to do :

@Value("#{securityProperties['cas.base.url']}")
private String SSO_CAS_BASE_URL;

That's why I like Spring.