Monday, August 26, 2013

Inheriting Bean in Spring Example


Inheriting Bean in Spring Example

By using the parent attribute of bean, we can specify the inheritance relation between the beans.
Such case parent bean values will be inherited to the current bean.

Steps as follows:
      1. Create Address.java and Employee.java
      2. Create ApplicationContext.xml
      3. Create executable class called ExecuteEmployee.java

1. Create Address.java

package com;
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;
    }
}

-------------------------------------------------

package com;
import java.io.Serializable;

public class Employee implements Serializable {

    private static final long serialVersionUID = 1L;
    private String empID;
    private String empName;
    private Address empAddress;

    Employee(String empID, String empName){
       super();
       this.empID = empID;
       this.empName = empName;
    }

    Employee(String empID, String empName, Address empAddress){
       super();
       this.empID = empID;
       this.empName = empName;
       this.empAddress = empAddress;
    }

    void displayEmployee(){
       System.out.println("Employee ID : " + empID);
       System.out.println("Employee Name : " + empName);
       System.out.println("Address : " + empAddress.displayAddressInfo());
    }
}

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="employee1Bean" class="com.Employee">
      <constructor-arg value="100100"></constructor-arg>
      <constructor-arg value="Sanjeeva Pathirana"></constructor-arg>
   </bean>

   <bean id="addressBean" class="com.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="employee2Bean" class="com.Employee" parent="employee1Bean">
      <constructor-arg ref="addressBean"></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 employee = new ExecuteEmployee();
     employee.doExecute();
   }

   private void doExecute(){
     Resource resource = new    ClassPathResource("resource/ApplicationContext.xml");
     BeanFactory beanFactory = new XmlBeanFactory(resource);
     Employee employee = (Employee)beanFactory.getBean("employee2Bean");
     employee.displayEmployee();
   }
}


Out put as follows:

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