Spring Bean Definition Inheritance & Bean Definition Template

Interactive Online Courses: Elevate Skills & Succeed Enroll Now!

1. Objective

This is the last article featuring the Spring Beans where you will be learning about the Spring Bean Definition Inheritance. You should know that a bean definition contains a lot of information about the constructor arguments, property values, and the IoC container-specific information. Moreover, we will see Spring Bean Definition Inheritance example using the Eclipse IDE.
So, let’s start Spring Bean Definition Inheritance.

Spring Bean Definition Inheritance with Bean Definition Template

Spring Bean Definition Inheritance with Bean Definition Template

2. Bean Definition Inheritance

The child beans definition inherits configuration data from the definition of the parent. Along with that the child definition has the ability to override some of the values while adding others when needed. The Java class inheritance has nothing to do with the Spring Bean definition but the concept of inheritance is applied. You have the power of defining the parent bean as a template and the other child beans can inherit the required configuration from the parent bean.
Let’s read about Java Inheritance in detail

3. Bean Definition Inheritance Example using Eclipse IDE

Let’s define a working example using Eclipse IDE in Bean Definition Inheritance having the following steps:

  1. Create your project with name SpringEx and a package com.example. This should be under the src folder of your created project.
  2. Add the Spring Libraries that are required using the Add External JARs options.
  3. Create HelloWorld.java, HelloIndia.java and MainApp.java under the above made package.
  4. Write the Beans.xml configuration file under the src folder.
  5. Finally write code for all Java files and Bean config file and run the application as described.

i. The code for the HelloWorld.java

package com.example; 
public class HelloWorld {
   private String message1;
   private String message2;
   public void setMessage1(String message){
      this.message1 = message;
   }
   public void setMessage2(String message){
      this.message2 = message;
   }
   public void getMessage1(){
      System.out.println("World Message1 : " + message1);
   }
   public void getMessage2(){
      System.out.println("World Message2 : " + message2);
   }
}

Read about Spring Transaction Management – Types and Methods

ii. The following is the code for HelloIndia.java

package com.example;
public class HelloIndia {
   private String message1;
   private String message2;
   private String message3;
   public void setMessage1(String message){
      this.message1 = message;
   }
   public void setMessage2(String message){
      this.message2 = message;
   }
   public void setMessage3(String message){
      this.message3 = message;
   }
   public void getMessage1(){
      System.out.println("India Message1 : " + message1);
   }
   public void getMessage2(){
      System.out.println("India Message2 : " + message2);
   }
   public void getMessage3(){
      System.out.println("India Message3 : " + message3);
   }
}

iii. MainApp.java is defined as follows

package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; 
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");   
      HelloWorld objA = (HelloWorld) context.getBean("helloWorld");
      objA.getMessage1();
      objA.getMessage2();
      HelloIndia objB = (HelloIndia) context.getBean("helloIndia");
      objB.getMessage1();
      objB.getMessage2();
      objB.getMessage3();
   }
}

Now after defining the Java files, you see the configuration file Beans.xml where HelloWorld beans which has properties message2 and message1. Next, the HelloIndia bean has to be defined as the child of HelloWorld by using parent attribute. Then the child beans inherit the message 2 property and override the message 1 property introducing another property message 3.
Have a look at Spring Beans Autowiring – Modes with Eclipse IDE Example
The Beans.xml is as follow:

<?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 = "helloWorld" class = "com.example.HelloWorld">
      <property name = "message1" value = "Hello World!"/>
      <property name = "message2" value = "Hello Second World!"/>
   </bean>
   <bean id =" helloIndia" class = "com.example.HelloIndia" parent = "helloWorld">
      <property name = "message1" value = "Hello India!"/>
      <property name = "message3" value = "Namaste India!"/>
   </bean>
</beans>

4. The Bean Definition Template

You also have the possibility of creating the Bean Definition Template which is used by the other child bean definitions without giving much effort. So while you are defining the Bean Definition Template you shouldn’t specify the class attribute but the abstract attribute. Also, you should specify the abstract attribute with the value “true” which is shown as below:

<?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 = "beanTeamplate" abstract = "true">
      <property name = "message1" value = "Hello World!"/>
      <property name = "message2" value = "Hello Second World!"/>
      <property name = "message3" value = "Namaste India!"/>
   </bean>
      <bean id = "helloIndia" class = "com.example.HelloIndia" parent = "beanTeamplate">
      <property name = "message1" value = "Hello India!"/>
      <property name = "message3" value = "Namaste India!"/>
   </bean>
</beans>

The parent Bean can’t be instantiated by its own as it is incomplete and is marked as abstract. Therefore, when the definition is abstract then its advisable to use it as a pure template Bean Definition only.
So, this was all about Spring Bean Definition Inheritance. Hope you like our explanation.

5. Conclusion

Hence, in this session, we learned Spring framework Beans Definition Inheritance. The more detailed analysis was done by showing a working example using Eclipse IDE. Also, without putting much effort you can create a Bean Definition Template which is used by the other child beans definition. Furthermore, if you have any query regarding Bean Definition Inheritance, feel free to ask in the comment section.
Related Topic – Spring BeanPostProcessors
For reference

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

Leave a Reply

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