Sunday, August 25, 2013

Dependency Injection by Constructor Example


Dependency Injection by Constructor Example

Simple example to inject primitive and String-based values.

Steps as follows:
      1. Create Employee.java POJO
      2. Create ApplicationContext emaple
      3. Create executable class called ExecuteEmployee.java
1. Create Employee.java POJO

package com;

import java.io.Serializable;


public class Employee implements Serializable {

   /**
   *
   */
   private static final long serialVersionUID = 1L;
   private int id;
   private String name;

   Employee(){} //default Constructor
   
   Employee(int id){ this.id = id; }
   
   Employee(String name){ this.name = name; }
   
   Employee(int id, String name){
     this.id = id;
     this.name = name;
   }

   void displayEmployee(){
     System.out.println("id | name : " + this.id + " | " + this.name);
   }
}

2. Create ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="employeeID" class="com.Employee">
<constructor-arg type="int" value="100"></constructor-arg>
</bean>
<bean id="employeeIDString" class="com.Employee">
<constructor-arg value="100"></constructor-arg>
</bean>
<bean id="employeeNameString" class="com.Employee">
<constructor-arg value="Sanjeeva"></constructor-arg>
</bean>
<bean id="employee" class="com.Employee">
<constructor-arg type="int" value="100"></constructor-arg>
<constructor-arg value="Sanjeeva"></constructor-arg>
</bean>
</beans>



3. Create ExecuteEmployee.java

package com;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class ExecuteEmployee {

   /**
   * @param args
   */
   public static void main(String[] args) {
     ExecuteEmployee exEmployee = new ExecuteEmployee();
     exEmployee.doExecute();
   }


   private void doExecute(){
     
     Resource resource = new    ClassPathResource("ApplicationContext.xml");
     BeanFactory beanFactory = new  XmlBeanFactory(resource);
     
     Employee employeeID = (Employee)beanFactory.getBean("employeeID");
     employeeID.displayEmployee();
     System.out.println("\nInjecting String-based values --> \n");

     Employee employeeIDString = (Employee)beanFactory.getBean("employeeIDString");
     employeeIDString.displayEmployee();
     System.out.println("--------------------------------- ");

     Employee employeeNameString = (Employee)beanFactory.getBean("employeeNameString");
     employeeNameString.displayEmployee();
     System.out.println("--------------------------------- ");

     Employee employee = (Employee)beanFactory.getBean("employee");
     employee.displayEmployee();
  }
}

Out put as follows:

Aug 26, 2013 8:48:21 AM
id | name : 100 | null

Injecting String-based values -->

id | name : 0 | 100
---------------------------------
id | name : 0 | Sanjeeva
---------------------------------
id | name : 100 | Sanjeeva