Home > Uncategorized > How to use PropertyPlaceholderConfigurer

How to use PropertyPlaceholderConfigurer

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer is a  bean factory post processor. This class can be used , when you want to provide some information to bean  configuration file from external resource(property file).
For example most of the time we used to hardcode the database related information directly in the bean configuration file itself. Using PropertyPlaceholderConfigurer we can configure the connection information such a way that, we will give some place holder in the bean configuration file .  Spring container pick up the value for that place holder from an external property file. This is the same concept we use in Ant.

Bean configuration
beanConfig.xml

<bean id="dataSource" destroy-method="close">
 <property name="driverClassName"><value>${db.driverClassName}</value></property>
 <property name="url"><value>${db.url}</value></property>
 <property name="username"><value>${db.username}</value></property>
 <property name="password"><value>${db.password}</value></property>
</bean>

Property file
Oracle.properties


db.driverClassName=oracle.jdbc.driver.OracleDriver
db.url=jdbc:oracle:thin:@db-server:1521:db-schema
db.username=username
db.password=password

How to configure this property file into  BeanConfig.xml

Simply configure PropertyPlaceholderConfigurer bean like other beans

<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="location"><value>Oracle.properties</value></property>
</bean>
<bean id="dataSource" destroy-method="close">
 <property name="driverClassName"><value>${db.driverClassName}</value></property>
 <property name="url"><value>${db.url}</value></property>
 <property name="username"><value>${db.username}</value></property>
 <property name="password"><value>${db.password}</value></property>
</bean>

Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.