Home » Java » Enterprise Java » Read property files with PropertyPlaceholderConfigurer

About Abhimanyu Prasad

Abhimanyu Prasad
Abhimanyu is a passionate tech blogger and senior programmer, who has an extensive end-to-end development experience with wide range of technologies. He is the founder and administrator at jCombat.

Read property files with PropertyPlaceholderConfigurer

1. Introduction

Usually when we are concerned with multiple servers where the application is deployed before going into production, we can configure the environment-specific parameters in an external property file. It might be the database details, which is different for test server and the production server. So it’s better we we choose to keep the database configuration file in an external property file. Similarly, we can choose to keep LDAP server details in an external property file. With property files in place, we don’t need to touch the configuration XML files, where the property file values can be directly picked up as ${name}.

All we need to do is to update the property file accordingly at every deployment, without even touching the Spring configuration context files.

In this tutorial, we will see how we can make use of PropertyPlaceholderConfigurer to read external property file values and access them from bean configuration in Spring.

2. Implementation

For a simple demo, let’s create a property file user.properties at some external location with the following content:


With that in place, lets make sure we have the application context file content as:


<beans xmlns="http://www.springframework.org/schema/beans"
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
	<bean id="helloWorld" class="com.jcombat.bean.HelloWorld">
		<property name="name" value="${name}" />

Note the highlighted section in the above snippet.

We have the main class below, which we will try to run.


package com.jcombat.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jcombat.bean.HelloWorld;
public class MainApp {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
		HelloWorld hellWorld = (HelloWorld) context.getBean("helloWorld");

3. Running the application

Running the above as java application displays as:


4. Download the source code

Reference: Read property files with PropertyPlaceholderConfigurer from our JCG partner Abhimanyu Prasad at the jCombat blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Notify of