Mock tests, Interview questions, Tutorials and Tech news
 
 
Home > Programming / tutorials > Reading properties file in Spring

Reading properties file in Spring

There are multiple ways to read properties file in Spring

Let us say you have database user name , password etc configured in jdbc.properties and would like to inject the values at run time.

Here is what will go in applicationContext.xml

<bean id=”propertyConfigurer”>
<property name=”locations”>
<list>
<value>classpath:environment.properties.properties</value>
</list>
</property>
</bean>

<bean id=”dataSource” destroy-method=”close”>
<property name=”driverClass” value=”${jdbc.driverClass}”/>
<property name=”jdbcUrl” value=”${jdbc.url}”/>
<property name=”user” value=”${jdbc.user}”/>
<property name=”password” value=”${jdbc.password}”/>
<property name=”minPoolSize” value=”${jdbc.minPoolSize}”/>
<property name=”maxPoolSize” value=”${jdbc.maxPoolSize}”/>
</bean>

and this will be your properties file

jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@abc.com:1521:ORAC
jdbc.user=hello
jdbc.password=hello
jdbc.minPoolSize=1
jdbc.maxPoolSize=10

At run time, the properties file value will be injected  into the applicationContext.xml

Be Sociable, Share!
  1. June 16th, 2011 at 21:25 | #1

    Thanks John.

  2. John
    June 16th, 2011 at 15:11 | #2

    The solutions is incomplete…

    You can user PropertyPlaceHolderConfigurer

    classpath:application.properties
    <!–
    file:/usr/config/web/application.properties
    file:C:/dev/config/web/application.properties
    –>

  1. No trackbacks yet.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes