বৃহস্পতিবার, ৮ ডিসেম্বর, ২০১১

Spring 3 Property file read

Often times, most Spring developers just put the entire deployment details (database details, log file path) in XML bean configuration file as following :

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">


<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/oushod"/>
<property name="user" value="userName"/>
<property name="password" value="password"/>

<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="20"/>
<property name="checkoutTimeout" value="300"/>
<property name="maxStatements" value="100"/>
</bean>


But, in a corporate environment, deployment detail is usually only can ‘touch’ by your system or database administrator, they just refuse to access your bean configuration file directly, and they will request a separate file for deployment configuration, for example, a simple properties, with deployment detail only.

To fix it, you can use PropertyPlaceholderConfigurer class to externalize the deployment details into a properties file, and access from bean configuration file via a special format – ${variable}.
Create a properties file (db.properties), include your database details, put it into your project class path.

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/oushod
user=root
password=admin


Declare a PropertyPlaceholderConfigurer in bean configuration file and map to the ‘db.properties‘ properties file you created just now.


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>



Full example

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">

<import resource="applicationException.xml"/>

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>

<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="20"/>
<property name="checkoutTimeout" value="300"/>
<property name="maxStatements" value="100"/>
</bean>

</beans>






















1 টি মন্তব্য: