Monday, August 26, 2013

Constructor Injection with Dependent-Object


Constructor-Injection-with-Dependent-Object

If there is HAS-A relationship between the classes, first we create the instance of dependent object and then pass it as an argument of the main class constructor.
    e.g. Employee HAS-A Address, so Address class object will be the dependent.

<bean id="addressBean" class="Address">
<constructor-arg value="126B, Pubudu Uyana"></constructor-arg>
<constructor-arg value="Piliyandala"></constructor-arg>
<constructor-arg value="Western Province"></constructor-arg>
<constructor-arg value="Sri Lanka"></constructor-arg>
</bean>
<bean id="employeeBean" class="Employee">
<constructor-arg type="int" value="100123"></constructor-arg>
<constructor-arg value="Sanjeeva"></constructor-arg>
<constructor-arg value="Pathirana"></constructor-arg>
<constructor-arg><ref bean="addressBean"/> </constructor-arg>
</bean>

Step 01 : Create Address.java

import java.io.Serializable;

public class Address implements Serializable {
   private static final long serialVersionUID = 1L;
   private String number;
   private String city;
   private String province;
   private String country;

   Address(String number, String city, String province, String country){
     super();
     this.number = number;
     this.city = city;
     this.province = province;
     this.country = country;
   }

   String displayAddressInfo(){
     return this.number + ", " + this.city + ", " + this.province + ", " + this.country;
   }
}


Step 02 : Create Address.java

import java.io.Serializable;


public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;
    private int id;
    private String firstname;
    private String lastname;
    private Address address;
 
    public Employee(int id, String fname, String lname, Address address) {
      super();
      this.id = id;
      this.firstname = fname;
      this.lastname = lname;
      this.address = address;
    }

    void displayEmployee(){
      System.out.println("Employee ID : " + id);
      System.out.println("Employee First Name : " + firstname);
      System.out.println("Employee Last Name : " + lastname);
      System.out.println("Employee Address : " + address.displayAddressInfo());
    }
}


Step 03 : 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="addressBean" class="Address">
<constructor-arg value="126B, Pubudu Uyana"></constructor-arg>
<constructor-arg value="Piliyandala"></constructor-arg>
<constructor-arg value="Western Province"></constructor-arg>
<constructor-arg value="Sri Lanka"></constructor-arg>
</bean>
<bean id="employeeBean" class="Employee">
<constructor-arg type="int" value="100123"></constructor-arg>
<constructor-arg value="Sanjeeva"></constructor-arg>
<constructor-arg value="Pathirana"></constructor-arg>
<constructor-arg><ref bean="addressBean"/></constructor-arg>
</bean>
</beans>


Step 04 : Create DisplayInformation.java

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 DisplayInformation {

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

   private void doExecute(){
     Resource resource = new ClassPathResource("ApplicationContext.xml");
     BeanFactory beanFactory = new XmlBeanFactory(resource);

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

Out Put as follows :

Employee ID : 100123
Employee First Name : Sanjeeva
Employee Last Name : Pathirana
Employee Address : 126B, Pubudu Uyana, Piliyandala, Western Province, Sri Lanka