Constructor arguments, property values, and container-specific information like initialization method, static factory method name, and so on may all be found in a bean definition. A parent definition’s configuration data is passed down to a child bean definition. The child definition has the ability to override or add values as needed.
Although Spring Bean’s definition of inheritance differs from Java class inheritance, the inheritance notion is the same. A parent bean definition can be used as a template, and other child beans can inherit the required configuration from it. When utilizing XML-based configuration metadata, the parent attribute is used to identify a child bean definition, with the parent bean as the value of this attribute.
We’ve previously seen one of the benefits of inheritance in Java: reusability. That is, if the base class has some attributes, the child class will inherit those properties and their values. In the Child class, we may also override those values. We can inherit the bean’s attributes and values in Spring, and we can also override them.
Implementation: Let us consider an arbitrarily class called Customer, so do the project structure is as follows:

Step 1: Creation of ‘Customer’ class
File: Customer.java
Java
package com.geeksforgeeks.beans.inheritance;
public class Customer {
private String name;
private String email;
private String country;
public String getName() { return name; }
public void setName(String name) { this .name = name; }
public String getEmail() { return email; }
public void setEmail(String email)
{
this .email = email;
}
public String getCountry() { return country; }
public void setCountry(String country)
{
this .country = country;
}
@Override public String toString()
{
return " Name:" + name + "\n Email:" + email
+ "\n Country:" + country;
}
}
|
Step 2: Create a spring beans.xml file that demonstrates how to inherit the bean.
File: beans.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< bean id = "baseCustomer" abstract = "true" >
< property name = "country" value = "India" />
</ bean >
< bean id = "customer" parent = "baseCustomer" class = "com.geeksforgeeks.beans.inheritance.Customer" >
< property name = "country" value = "India" />
< property name = "name" value = "Admin" />
< property name = "email" value = "geeksforgeeks@gmail.com" />
</ bean >
</ beans >
|
We have defined 2 beans in the configuration file
- Bean with the id base.
- The customer is the parent bean, while the second bean with the same id is the child bean. As a result, the country property and its value are inherited by the child bean customer, who then adds two additional properties to it.
Step 3: Create a class that loads these two beans and displays the output with the inherited value.
Java
package com.geeksforgeeks.beans.inheritance;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BeanInheritanceTest {
public static void main(String[] args)
{
ClassPathXmlApplicationContext applicationContext
= new ClassPathXmlApplicationContext(
"beans.xml" );
Customer customer
= (Customer)applicationContext.getBean(
"customer" );
System.out.println(customer.toString());
}
}
|
Step 4: Run the above class and see the output
Only the Country attribute of the parent bean has value. Even though we only introduced two attributes to Child, it contains values for all three properties. As a result, the Country property has been effectively passed from the Parent bean to our Child bean.

Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
19 Mar, 2022
Like Article
Save Article