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.

spring-config.xml

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

<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}"/>
</bean>

jdbc.properties

jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=dbadmin
jdbc.password=root

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;
 
  @Override
  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/jdbc.properties"/>

References

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html

http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/ApplicationContextAware.html

Advertisements

, , , ,

  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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: