Spring PropertyPlaceholderConfigurer example

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-2.5.xsd">

	<bean id="customerDAO" class="com.mkyong.customer.dao.impl.JdbcCustomerDAO">

		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="customerSimpleDAO" class="com.mkyong.customer.dao.impl.SimpleJdbcCustomerDAO">

		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">

		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/mkyongjava" />
		<property name="username" value="root" />
		<property name="password" value="password" />
	</bean>

</beans>

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.

PropertyPlaceholderConfigurer example

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 (database.properties), include your database details, put it into your project class path.


	jdbc.driverClassName=com.mysql.jdbc.Driver
	jdbc.url=jdbc:mysql://localhost:3306/mkyongjava
	jdbc.username=root
	jdbc.password=password

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


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

		<property name="location">
			<value>database.properties</value>
		</property>
	</bean>

Full example


<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-2.5.xsd">

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

		<property name="location">
			<value>database.properties</value>
		</property>
	</bean>

	<bean id="customerDAO" class="com.mkyong.customer.dao.impl.JdbcCustomerDAO">

		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="customerSimpleDAO" 
                class="com.mkyong.customer.dao.impl.SimpleJdbcCustomerDAO">

		<property name="dataSource" ref="dataSource" />
	</bean>

	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">

		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

</beans>
Alternative usage
You also can use PropertyPlaceholderConfigurer to share some constant variables to all other beans. For example, define your log file location in a properties file, and access the properties value from different beans configuration files via ${log.filepath}.

Download Source Code

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Jesse
Guest
Jesse

do we need to use beans xml if we are using spring boot?

Tarandeep Singh
Guest
Tarandeep Singh

When do we use #{…} instead if ${…} and how both are different. Also please explain what does it mean – #{${..}}

harshana dias
Guest
harshana dias

Where is the applicability of the location variable? I only see the define point of it.

Igor Gorovoy
Guest
Igor Gorovoy

How to include property file in groovy.config file

yd
Guest
yd

Thats really useful!! Many Many Thanks!!!!! :D

Ercan Çelik
Guest
Ercan Çelik

Hi,
I have a base project that contains base services(written in spring). I have also a spring mvc project that has a maven dependency to base spring project. Base spring project has property files and the other spring mvc project has property files too. But if i didn’t redefine base project’s property files in spring mvc project it throws error. So i redefine the base projects property files also in spring mvc project. Is there a way to define property files only in its project?

Sriram
Guest
Sriram

Hi,
This example states how to get the values inside the configuration file. Using PropertyPlaceholderConfigurer, How to get the property values inside a java class.

QZ
Guest
QZ

Hello, you can use Environment. Here is the example:

@PropertySource(“some property file”)
class A{

@Autowired
Environment env;

public void setProperties{

String property = env.getgetProperty(“Property name”);

}

}

yathirigan
Guest
yathirigan

If i chose to place the property file external to the application (EAR/WAR), and when the property file changes, would restart the application get the new property to be in effect ?

Ramesh
Guest
Ramesh

I am trying to supply a runtime name of the file (my message queue.properties file differs for my different environment)

But I am not able to set the $hostname} with the name of my hostname for spring application context to load it dynamically.

If you have pointers for the same that would be really helpful

Springa Head
Guest
Springa Head

You forgot to say where to deploy the properties file! Your post is pretty close to useless..

Pit
Guest
Pit

just great!!. thanks

trackback
Secure your Java Spring app with PropertyPlaceholderConfigurer » Robert Blackburn

[…] want soem more inforkation and example of how to leverage PropertyPlaceholderConfigurer, check out MKYong.com’s PropertyPlaceholderConfigurer Example. Share and […]

raghavendra
Guest
raghavendra

how to read properties file using spring…

Arvin Rong
Guest
Arvin Rong

Is there any way to init a Bean’s property of Map type using properties file?I have searched for it for a whole day?but I found none. Thx a lot. Looking forward to your reply.

Nivesh Sengar
Guest
Nivesh Sengar

Why not u can put the values in Property file like below
key1=Value1
key2=Value2
key3=Value3

And then assign this in the Spring configured XML like

<bean id = "someClass" class="somePackage.SomeClass">
<property name=""someMap>
<map>
<entry key="key1" value="${key1}" />
<entry key="key1" value="${key1}" />
<entry key="key1" value="${key1}" />
</map>
</property>
</bean>
Kumar
Guest
Kumar

Thanks a lot for the crisp and useful article

satanrulz
Guest
satanrulz

can we have PropertyPlaceholderConfigurer read properties file from the file system?

Luis Bacca
Guest
Luis Bacca

Yes, using file:relativepath or file:absolutepath both pointing to your properties file in the filesystem.

Levan
Guest
Levan

Thanks again ))
Got just what I wanted in seconds

Alex
Guest
Alex

This example was really helpful, thanks a lot for the post. This gave me just what I needed.

trackback
Update database settings in properties file in Spring - Question Lounge

[…] details on page load and updates the database settings when the user press submitI followed this tutorial and set the database settings in properties file.I managed to update the database settings in […]

trackback
Update database settings in properties file in Spring - - Coding Answers

[…] followed this tutorial and set the database settings in properties […]

trackback
Photo Birth Announcements and Birth Baby Announcements | ratedbabyproducts.com

[…] Spring – PropertyPlaceholderConfigurer example | Spring […]