Bean Inheritance

Bean Inheritance

In this tutorial, we are going to discuss bean inheritance in the Spring framework. In spring, inheritance is supported for reusing already written bean. So that beans can share common attributes and methods among them.

Bean Inheritance

In general, in the Spring framework, we can provide more and more no of configuration details in beans definitions under beans configuration file like properties, configurations, dependency injection related configurations, initialization destruction methods configurations etc.

In the above context, we can reuse one bean configuration details in another bean configuration like normal java classes inheritance to improve reusability and to reduce the number of configurations in the spring configuration file by declaring parent and child beans configurations.

To declare a particular bean configuration as a parent to the present bean configuration, then we have to use the “parent” attribute in the “<bean>” tag in the beans configuration file, where the “parent” attribute will take the identity of the respective bean definition.

E.g

package com.ashok.spring.core.bean.inheritance.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private String empAddress;
   private double salary;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public String getEmpAddress() {
      return empAddress;
   }

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + salary
            + "]";
   }
}
<?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 = "emp1" class = "com.ashok.spring.core.bean.inheritance.beans.Employee">
      <property name = "empName" value = "Ashok Kumar"/>
      <property name = "empId" value = "Emp0087"/>
      <property name = "empAddress" value = "Bhimavaram"/>
      <property name = "salary" value = "50000"/>
   </bean>

   <bean id = "emp2" class = "com.ashok.spring.core.bean.inheritance.beans.Employee" parent="emp1">
      <property name = "empName" value = "Anand Kumar"/>
      <property name = "empId" value = "Emp0086"/>
   </bean>
</beans>
package com.ashok.spring.core.bean.inheritance.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.core.bean.inheritance.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/bean/inheritance/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp1 = (Employee) context.getBean("emp1");
      System.out.println(emp1);
      Employee emp2 = (Employee) context.getBean("emp2");
      System.out.println(emp2);
   }
}

Output

Employee [empName=Ashok Kumar, empId=Emp0087, empAddress=Bhimavaram, salary=50000.0]
Employee [empName=Anand Kumar, empId=Emp0086, empAddress=Bhimavaram, salary=50000.0]

In the above Beans Inheritance, it is possible to declare parent bean definition as a template, that is, a set of configurations which we want to apply to child definitions, not to have its own bean class and not to apply to its own bean class.

To declare a bean definition as template we have to use “abstract” attribute with “true” value in <bean> tag, in this context, it is not required to use “class” attribute in bean tag.

E.g

package com.ashok.spring.core.bean.inheritance.withabstract.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private String empAddress;
   private double salary;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public String getEmpAddress() {
      return empAddress;
   }

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }

   public double getSalary() {
      return salary;
   }

   public void setSalary(double salary) {
      this.salary = salary;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", salary=" + salary
            + "]";
   }
}
<?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 = "parnet_emp" class = "com.ashok.spring.core.bean.inheritance.withabstract.beans.Employee" abstract="true">
      <property name = "empAddress" value = "Bhimavaram"/>
      <property name = "salary" value = "50000"/>
   </bean>
   <bean id = "emp1" class = "com.ashok.spring.core.bean.inheritance.withabstract.beans.Employee" parent="parnet_emp">
      <property name = "empName" value = "Ashok Kumar"/>
      <property name = "empId" value = "Emp0087"/>
   </bean>
   <bean id = "emp2" class = "com.ashok.spring.core.bean.inheritance.withabstract.beans.Employee" parent="parnet_emp">
      <property name = "empName" value = "Anand Kumar"/>
      <property name = "empId" value = "Emp0086"/>
   </bean>
</beans>
package com.ashok.spring.core.bean.inheritance.withabstract.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.core.bean.inheritance.withabstract.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/bean/inheritance/withabstract/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp1 = (Employee) context.getBean("emp1");
      System.out.println(emp1);
      Employee emp2 = (Employee) context.getBean("emp2");
      System.out.println(emp2);
   }
}

Output

Employee [empName=Ashok Kumar, empId=Emp0087, empAddress=Bhimavaram, salary=50000.0]
Employee [empName=Anand Kumar, empId=Emp0086, empAddress=Bhimavaram, salary=50000.0]
Bean Inheritance
Scroll to top