Menu

Wednesday, November 2, 2011

Inject properties file into Bean using spring framework

Step 1: Download the latest spring framework from the official site http://www.springsource.org/download and extract the distribution. The necessary jar files for this example is.

org.springframework.asm-3.1.0.RC1.jar
org.springframework.beans-3.1.0.RC1.jar
org.springframework.context-3.1.0.RC1.jar
org.springframework.core-3.1.0.RC1.jar
org.springframework.expression-3.1.0.RC1.jar
commons-logging-1.1.1.jar

Note: The spring framework jar files will be in spring-framework-distribution/dist folder

Step 2: Create a Java Project named “SimpleSpring”.

File --> New --> Java Project 
    Create config folder to keep all the spring configuration related xml files.
    Create lib folder to keep all the necessary jar files.
    Create src/main folder to keep the java files.
    Create src/resources folder to keep resources related files.

Add these folders and the jar files in the project’s classpath.
To do this, just right click on the project --> Properties --> Java Build Path



   The final Structure of the project is like this.

Step 3: Create default.properties file under the resources folder and add the following. These values will be injected into Bean using Spring Framework.
      username=testuser
      password=testpasswd

Step 4: Create spring-config.xml in the config folder and the following.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
  
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
        <list>
          <value>classpath:default.properties</value>          
        </list>
      </property>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
  </bean>
  
  <!-- Inject the properties of default.properties file into appConfig instance -->
  <bean id="appConfig" class="ae.gov.dmi.polopoly.AppConfig">
   <property name="userName" value="${username}"></property>
   <property name="password" value="${password}"></property>
  </bean>  
</beans>


The Spring Framework reads information from the default.properties file and inject into the appConfig instance.

Step 5: Create AppConfig java file
package com.spring.example;

public class AppConfig {
 private String userName;
 private String password;
 
 public String getUserName() {
  return userName;
 }
 
 public void setUserName(String userName) {
  this.userName = userName;
 }
 
 public String getPassword() {
  return password;
 }
 
 public void setPassword(String password) {
  this.password = password;
 } 
} 

Step 6: Create Main class and add the following
package com.spring.example;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
 
 private static final Logger logger = Logger.getLogger(Main.class);
 
 public static void main (String[] args){
  
  logger.info("Starts");
  ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
  
  AppConfig appConfig = (AppConfig) context.getBean("appConfig");
  
  System.out.println(appConfig.getUserName());
  logger.info("Ends");
 }
}


Step 7: Run the Main class. This program outputs the username which is configured in the default.properties.