Categories
Spring Spring Core

Bean Definition Inheritance

Inheritance in Java : To Inherit all the Properties and Methods of a Parent Class to Sub-Class.

The same definition applies to the Spring Bean Definition Inheritance concept. Child Bean Definition can inherit the Constructor-args, property values, static factory method name, init-method name and so on from the Parent Bean Definition.

    <bean id="parentBean" class="example.ParentBean">  
        <property name="empName" value="Ritu"></property>
        <property name="age" value="21"></property>
    </bean>
    <bean id="childBean" class="example.ChildBean" parent="parentBean">
        <property name="age" value="22"></property>
    </bean>

From the above snippet have you noticed the use of Parent attribute from Child Bean Definition. Here the Child Bean Definition inherits all the properties from Parent Bean and overrides age property with its own.

Spring Bean definition inheritance with example
Bean Definition Inheritance

As per the above Diagram, we can inherit a Parent Bean Definition to more than one Child Bean Definition. 

We shall look at the working example for better understanding.

Bean Definition Inheritance Example

Step 1 : Create a Mvn Project named BeanInheritance in Eclipse IDE.

Step 2 : Adding Dependencies

Open pom.xml file and add the below mentioned dependencies.

<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>5.2.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>5.2.6.RELEASE</version>
		</dependency>

Step 3 : Create a Class named ChildBean and copy the code below.

public class ChildClass {
	private String dataSource;
	private String empName;
	private int age;
	private String role;
	
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		System.out.println("Child employee name : "+empName);
		this.empName = empName;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		System.out.println("Age : "+age);
		this.age = age;
	}
	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		System.out.println("Role : "+role);
		this.role = role;
	}
	public String getDataSource() {
		return dataSource;
	}
	public void setDataSource(String dataSource) {
		System.out.println("From Parent Bean Definition -- Child Class DS : "+dataSource);
		this.dataSource = dataSource;
	}
}

I have DataSource, EmpName, Role and Age properties in the above class. In which DataSource property value will be inherited from Parent Bean Definition.

Step 4 : Starter class to run the application

Create StarterClass and configure IoC Container and make a call to ChildBean as below.

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class StarterClass {
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		applicationContext.getBean("childBean", ChildClass.class);
	}
}

Step 5 : Bean Definition

Create applicationContext.xml in the root folder and copy below code

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="beanTemplate" abstract="true">
		<property name="dataSource" value="mySqlDS"></property>
	</bean>
	<bean id="childBean" class="ChildClass" parent="beanTemplate">
		<property name="empName" value="Ritu"></property>
		<property name="age" value="22"></property>
		<property name="role" value="Developer"></property>
	</bean>
</beans>

Here in the above Bean Definition, I defined the parent bean as abstract and added one property called dataSource to share across its Inherited bean definitions.

The childBean definition will get instantiated with parent property dataSource with its other own properties.

Run the above program from StarterClass you can see the below output in console.

The above program inherited the Parent Bean Definition and configured it with Parent and it’s own properties. I hope you are now clear on the usage of Bean Inheritance concepts.

Key Terms :

  • The Child Bean Definition Inherits data from Parent Bean Configuration
  • The Child Definition can override the values of Parent Definition
  • It is a Form of Templating
  • Reusing the Bean definition makes time efficiency 
  • If class attribute is not specified in Child Bean Definition, it will make use of the Parent Class
  • Child Bean class should be compatible with the parent bean definitions. Otherwise it will throw an exception.
  • Child class won’t inherit “depends-on”, “autowire mode”, “lazy-init” properties from parent bean definition.
  • It is good practice to always use the parent bean as abstract and define no class in it.
  • When you mark a bean as abstract, then it is purely used as a template to other bean definitions. You cannot directly call the bean or ref from another bean. 
  • If the Bean is defined as abstract, IoC Container will not configure it by default.
  • You can inherit the parent bean definition from more than one Child bean definitions.

That’s all for now on Spring Bean Definition Inheritance with a working example.

Happy Reading!!!

Leave a Reply

Your email address will not be published. Required fields are marked *