Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Spring Bean defines inheritance


May 14, 2021 Spring


Table of contents


Bean defines inheritance

Bean definitions can contain a lot of configuration information, including constructor parameters, property values, container-specific information such as initialization methods, static factory method names, and so on.

The definition of a child bean inherits the configuration data defined by the parent. Subdides can override some values as needed, or add additional values.

Spring Bean defines inheritance independently of java class inheritance, but the concept of inheritance is the same. You can define a parent bean as a template and other child beans to inherit the desired configuration from the parent bean.

When you use XML-based configuration metadata, the definition of a child bean is indicated by specifying the parent bean as the value of the property by using the parent property.

Example

We use the Eclipse IDE in the right place, and then follow these steps to create a Spring application:

Steps Describe
1 Create a project named SpringExample, and create a package com.tutorialspoint in the src folder that created the project.
2 Use the Add External JARs option to add the Spring library you want, as explained in the Spring Hello World Example section.
3 Create Java-like HelloWorld, HelloIndia, and MainApp in the com.tutorialspoint package.
4 Create the Beans profile Beans file in the src .xml.
5 The final step is to create the contents of all Java files and bean profiles and run the application as explained below.

Here's .xml the profile Beans, in which we define the "helloWorld" bean with two properties, message1 and message2. T hen, use the parent property to define the "helloIndia" bean as the child of the "helloWorld" bean. This sub-bean inherits the property of message2, overrides the property of message1, and introduces a property, message3.

<?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.tutorialspoint.HelloWorld">
      <property name="message1" value="Hello World!"/>
      <property name="message2" value="Hello Second World!"/>
   </bean>

   <bean id="helloIndia" class="com.tutorialspoint.HelloIndia" parent="helloWorld">
      <property name="message1" value="Hello India!"/>
      <property name="message3" value="Namaste India!"/>
   </bean>

</beans>

Here is the contents .java HelloWorld file:

package com.tutorialspoint;
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);
   }
}

Here's what HelloIndia .java file:

package com.tutorialspoint;

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);
   }
}

Here's what .java mainApp files are:

package com.tutorialspoint;

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();
   }
}

Once you've created the source code and bean profile, we're ready to run the application. If everything is fine with your application, the following information will be output:

World Message1 : Hello World!
World Message2 : Hello Second World!
India Message1 : Hello India!
India Message2 : Hello Second World!
India Message3 : Namaste India!

Here you can observe that we created the "helloIndia" bean without passing message2, but because of the inheritance defined by the bean, it passed message2.

Bean defines the template

You can create a bean definition template that can be used by other sub-bean definitions without much effort. When you define a bean definition template, you should not specify the properties of the class, but instead specify the abstract property with the true value, as follows:

<?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.tutorialspoint.HelloIndia" parent="beanTeamplate">
      <property name="message1" value="Hello India!"/>
      <property name="message3" value="Namaste India!"/>
   </bean>

</beans>

The parent bean itself cannot be instantiated because it is incomplete, and it is explicitly marked as abstract. When a definition is abstract, it is used only as a pure template bean definition, acting as a parent definition for child definitions.