Configuring project properties in Spring

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.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.