Spring PropertyPlaceholderConfigurer using Annotation

Spring 3.1 introduces the new PropertySourcesPlaceholderConfigurer that replaces the old XML <context:property-placeholder>

This replacement class is created to be more flexible and to better interact with the newly introduced Environment and PropertySource mechanism.

Following example demonstrates PropertyPlaceholderConfigurer using Annotation:

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml





Create Employee Class as following

package com.kruders.bean;

public class Employee {
	private String name;
	public String getName() {
		return name;
	public void setName(String name) {
		this.name = name;

2. Spring Configuration

Create Config.java configures all the bean using Annotation that was earlier XML based

package com.kruders.config;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import com.kruders.bean.Employee;
import com.kruders.dao.EmployeeDao;

public class Config {
	public static PropertyPlaceholderConfigurer properties(){
	  PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
	  ClassPathResource[] resources = new ClassPathResource[ ]
	    { new ClassPathResource( "db.properties" ) };
	  ppc.setLocations( resources );
	  ppc.setIgnoreUnresolvablePlaceholders( true );
	  return ppc;
	@Value( "${jdbc.url}" ) private String jdbcUrl;
	@Value( "${jdbc.driverClassName}" ) private String driverClassName;
	@Value( "${jdbc.username}" ) private String username;
	@Value( "${jdbc.password}" ) private String password;
    public DataSource datasource() {
		BasicDataSource ds = new BasicDataSource();
		return ds;
    public Employee employee() {
		Employee employee = new Employee();
        return employee;
	public EmployeeDao employeeDao() {
		EmployeeDao employeeDao =  new EmployeeDao();
		return employeeDao;

PropertyPlaceholderConfigurer added in Spring 3.1 resolve ${…} placeholders within bean definition property values and @Value annotations.

For example, to inject a property using the @Value annotation:

@Value( "${jdbc.url}" ) private String jdbcUrl;

3. Properties File

Create db.properties that contains your database details


4. Run Program

Create Main.java class that inserts the employee details and run it as Java Application

package com.kruders.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kruders.bean.Employee;
import com.kruders.dao.EmployeeDao;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Beans.xml");
		Employee employee = (Employee) context.getBean("employee");

		System.out.println("Name " + employee.getName());
		EmployeeDao employeeDAO = (EmployeeDao) context.getBean("employeeDAO");


The folder structure of the example is shown below in Figure 49.1

Figure 49.1 Figure 49.1

You can download the source code of this example here.


No comments yet.

Leave a Reply