Configuring and using properties file in spring 3

Here in this tutorial we see a couple of ways to configure and use properties file in a spring application.

1st Method

Using properties file in spring is simple. Configure it in your spring config file as below.


<bean id="props" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:com/foo/"/>

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>


In your spring config file the string ${jdbc.username} is replaced at runtime with the value ‘dbadmin’, and the same applies for other placeholder values that match keys in the properties file.

ApplicationContextAware: When an ApplicationContext creates a class that implements the org.springframework.context.ApplicationContextAware interface, the class is provided with a reference to that ApplicationContext.

public interface ApplicationContextAware {
  void setApplicationContext(ApplicationContext applicationContext) throws BeansException;

you can implement this interface in your application as below:

package com.bharatonjava.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextUtils implements ApplicationContextAware {
  private static ApplicationContext context;
  public void setApplicationContext(ApplicationContext appContext)
      throws BeansException {
    context = appContext;
  public static ApplicationContext getApplicationContext() {
    return context;

Now using above utility class you can access any bean instance configured in your application context. So in your source code you can get an instance of properties file as below.

ApplicationContext appCtx = ApplicationContextUtils.getApplicationContext();
Properties props = (Properties) appCtx.getBean("props");

You can use above utility class to get reference to any bean configured in your spring configs.

2nd Method

The context namespace was introduced in Spring 2.5, it is possible to configure property placeholders
with a dedicated configuration element. One or more locations can be provided as a comma-separated
list in the location attribute.

<context:property-placeholder location="classpath:com/foo/"/>



, , , ,

  1. #1 by java2novice on June 29, 2014 - 4:14 PM

    very nice

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: