tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 
 Spring > Core > Bean Definition Inheritance3

Bean Definition Inheritance3 

Spring Inversion of Control (IoC) also known as Dependency Injection (DI) is a process by which objects define their dependencies with collaborating objects. The following example shows bean definition inheritance. A bean can inherit from other bean definitions by specifying parent attribute. The child bean definition may redefine some or all properties if required.

File Name  :  
/SpringCore001/conf/inheritance/inheritance3.xml 

File Name  :  
com/bethecoder/tutorials/spring3/basic/Employee.java 
   
package com.bethecoder.tutorials.spring3.basic;

public class Employee {
  
  private int id;
  private String name;
  private double salary;
  
  public Employee() {
    super();
    this.id = -1;
    this.name = "Unknown";
    this.salary = 0;
  }
  
  public Employee(int id, String name) {
    super();
    this.id = id;
    this.name = name;
    this.salary = 20000;
  }
  
  public Employee(int id, String name, double salary) {
    super();
    this.id = id;
    this.name = name;
    this.salary = salary;
  }

  public int getId() {
    return id;
  }
  
  public void setId(int id) {
    this.id = id;
  }
  
  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public double getSalary() {
    return salary;
  }
  
  public void setSalary(double salary) {
    this.salary = salary;
  }
  
  public String toString() {
    return "Employee[" + id + ", " + name + ", " + salary + "]";
  }
  
}
   

File Name  :  
com/bethecoder/tutorials/spring3/basic/Manager.java 
   
package com.bethecoder.tutorials.spring3.basic;

import java.util.List;

public class Manager extends Employee {

  private List<Employee> subOrdinates = null;

  public List<Employee> getSubOrdinates() {
    return subOrdinates;
  }

  public void setSubOrdinates(List<Employee> subOrdinates) {
    this.subOrdinates = subOrdinates;
  }
  
  public String toString() {
    return "Manager[" + getId() ", " 
      getName() ", " + getSalary() "] => " + subOrdinates;
  }
}
   

File Name  :  
com/bethecoder/tutorials/spring3/tests/inheritance/PureTemplate.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.spring3.tests.inheritance;

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

import com.bethecoder.tutorials.spring3.basic.Employee;
import com.bethecoder.tutorials.spring3.basic.Manager;

public class PureTemplate {

  /**
   @param args
   */
  public static void main(String[] args) {
    XmlBeanFactory factory = new XmlBeanFactory(
                new ClassPathResource("inheritance3.xml"));

    Employee emp1 = (Employeefactory.getBean("emp1");
    System.out.println(emp1);
    
    Employee emp2 = (Employeefactory.getBean("emp2");
    System.out.println(emp2);
    
    Manager mgr = (Managerfactory.getBean("mgr");
    System.out.println(mgr);
  }

}
   

It gives the following output,
Employee[1, XYZ, 90000.0]
Employee[2, PQR, 90000.0]
Manager[1, MANAGER1, 90000.0] => [Employee[1, XYZ, 90000.0], Employee[2, PQR, 90000.0]]


                    Print

   



Books Recommendation

 
 
bl  br