Open In App
Related Articles

Spring – Inheriting Bean

Improve Article
Save Article
Like Article

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 



// Java Program to Illustrate Customer Class
package com.geeksforgeeks.beans.inheritance;
// Class
public class Customer {
    // Class data members
    private String name;
    private String email;
    private String country;
    // Getters and setters
    public String getName() { return name; }
    public void setName(String name) { = name; }
    public String getEmail() { return email; }
    public void setEmail(String email)
        // this keyword refers to current instance itself = email;
    // Method
    public String getCountry() { return country; }
    // Setter
    public void setCountry(String country)
    { = country;
    // Method
    // To show message
    @Override public String toString()
        // Print corresponding customer attributes
        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 version="1.0" encoding="UTF-8"?>
    <bean id="baseCustomer" abstract="true">
    <property name="country" value="India"/>
    <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=""/>


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 Program to Illustrate Bean Inheritance
package com.geeksforgeeks.beans.inheritance;
// Importing required classes
// Class
public class BeanInheritanceTest {
    // Main driver method
    public static void main(String[] args)
        // Inheriting Bean by customer
        ClassPathXmlApplicationContext applicationContext
            = new ClassPathXmlApplicationContext(
        Customer customer
            = (Customer)applicationContext.getBean(
        // Printing the customer info


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
Similar Reads
Complete Tutorials