BitterSweetJaVa

August 11, 2009

Property Reader in Spring Framework

Filed under: Java — Tags: , , — .|2ic|K @ 9:55 AM

In order to add support to this feature, we must add inside the Spring Context file, the following bean definition:


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>/WEB-INF/classes/MyPropertyFile.properties</value>
    </property>
</bean>

Where MyPropertyFile.properties is the file that has stored the properties we need to get.
By adding the definition above, we would be able to bind each one of the properties from the file to a particular bean:

<bean id="MyAppService" class="com.application.MyAppService">
    <property name="fileNameProd">
        <value>${application.fileName.prod}</value>
    </property>
    <property name="fileNameQA">
        <value>${application.fileName.qa}</value>
    </property>
    <property name="fileNameUAT">
        <value>${application.fileName.uat}</value>
    </property>
</bean>

When we get the MyAppService Bean, the properties will be already loaded from our file.

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.