Wednesday, 20 April 2016

How to use Property placeholder in Spring XML to load from properties file ?


If you need to read properties file in your Spring application, all you need is to configure a PropertyPlaceholderConfigurer bean in your application context.

Setup your properties in a property file (Example: project.properties)

# project.properties - DB Info
jdbc.driver=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb: db/myapp
jdbc.user=sa
jdbc.password=xsx
jdbc.maxConnections=25

Setup and use these properties in Spring configuration XML file
<bean id="propertyConfigurer"
 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:project.properties</value>
     </property>
</bean>

<bean id="dataSource" 
 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName">
        <value>${jdbc.driver}</value>
    </property>
    <property name="url">
         <value>${jdbc.url}</value>
    </property>
    <property name="username">
         <value>${jdbc.user}</value>
    </property>
    <property name="password">
         <value>${jdbc.password}</value>
    </property>
</bean>

No comments:

Post a Comment

Note: only a member of this blog may post a comment.