Sunday, January 11, 2015

Reading properties file with Spring framework

Good day, I'd learned a new way of reading properties file with Spring. Usually when setting up a project with Spring framework, one common thing should have in Spring is following code:
   <bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:config/application.properties</value>
                <value>classpath:config/database/database.properties</value>
            </list>
        </property>
   </bean>

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

        <property name="properties" ref="applicationProperties" />

    </bean>

I guess that was the classic design of a Spring project. Let's assume the application.properties file containing following content:

input.path=./
output.path=./

In order to read that properties, this is what I did in a my class:
public class MyClass
   protected Logger log = Logger.getLogger(this.getClass().getName());

   @Value("#{applicationProperties['input.path']}")
   private String filePath;
   ...
   ...
}

No comments: