Archive for July, 2009

Configuring project properties in Spring

July 6, 2009

It is useful to have all project properties in a text file and make Spring care of objects initialisation, especially if you have multiple environments (dev, stage, prod, etc). For this, I use placeholders like ${mybean.itsproperty} in spring-config.xml – just add a couple of configurers there:

< bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

< property name='location'>< value>config/common.properties< /value>< /property>

< /bean>

< bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">

< property name='location'>< value>config/env.properties< /value>< /property>

< /bean>

Then I can use placeholders like:

< bean id="coolDS" class="org.springframework.jdbc.datasource.DriverManagerDataSource">

< property name="driverClassName">< value>${coolDS.driverClassName}< /value>< /property>

...

< /bean>

< bean id="config" class="com.mycompany.Config" scope="singleton">

< property name="incoming_directory">< value>${config.incoming_directory}< /value>< /property>

...

< /bean>

Note that placeholders should be in format beanName.propertyName if you want to override some environment-specific properties.