Skip to content
Related Articles
Open in App
Not now

Related Articles

Spring – Inheriting Bean

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 19 Mar, 2022
Improve Article
Save 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 

File: Customer.java

Java




// 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) { this.name = name; }
 
    public String getEmail() { return email; }
    public void setEmail(String email)
    {
        // this keyword refers to current instance itself
        this.email = email;
    }
 
    // Method
    public String getCountry() { return country; }
 
    // Setter
    public void setCountry(String country)
    {
        this.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




<?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




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

 

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!