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

  <dependencies>
   	<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.1.2.RELEASE</version>
    </dependency>
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>5.1.21</version>
    </dependency>
    <dependency>
	 	<groupId>commons-dbcp</groupId>
	 	<artifactId>commons-dbcp</artifactId>
	 	<version>1.4</version>
	</dependency>
	
    <dependency>
		<groupId>cglib</groupId>
		<artifactId>cglib</artifactId>
		<version>2.2.2</version>
	</dependency>

  </dependencies>

1. POJO

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;

@Configuration
public class Config {
	@Bean
	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;
	
	@Bean(name="datasource")
    public DataSource datasource() {
		BasicDataSource ds = new BasicDataSource();
		ds.setDriverClassName(driverClassName);
		ds.setUrl(jdbcUrl);
		ds.setUsername(username);
		ds.setPassword(password);
		return ds;
    }
	
	@Bean(name="employee")
    public Employee employee() {
		Employee employee = new Employee();
		employee.setName("Sahil");
        return employee;
    }
	
	@Bean(name="employeeDao")
	public EmployeeDao employeeDao() {
		EmployeeDao employeeDao =  new EmployeeDao();
		employeeDao.setDataSource(datasource());
		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

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

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");
        employeeDAO.insert(employee);
	}

}

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