There are several places in the Spring configuration that can make use of variable substitution from a property file. First things first. If you create a property file how do you access it from your spring configuration file? Near the top of the configuration file add the following bean definition:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:my-project.properties</value>
</property>
</bean>
If you need to have comments in the property file, start the line with a #. Properties should be entered into the property file in the following format: property.name=property value, i.e.
my.favorite.search.engine=http://www.google.com
Properties can be referenced from value fields, as well as bean variables.
<bean id="jmsQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${my.project.testQueue}"/>
<property name="jndiTemplate" ref="jndiTemplate" />
</bean>
<si-ws:outbound-gateway id="WebServiceGateway"
request-channel="jmsChannel"
reply-channel="replyChannel"
uri="${my.project.uri}" />
It is also possible to access properties from spring annotations which can be very useful. The scheduled annotation is a good example.
@Scheduled(cron="${my.project.schedule.cron}")
Spring makes it very easy to leverage properties files for making systems easier to configure on the fly. There is no need to continue hard coding values that you or your client may want to change.
1 comment:
Hi,
I am trying to use placeholderconfigurer in spring, but i want the properties file outside jar.ie., properties file is in same fodler as jar. How do i specify the location in placeholderconfigurer bean of spring
Post a Comment