Spring

Spring - Bean Definition Inheritance

Spring - Bean Definition Inheritance

A bean definition can contain many design data, including constructor arguments, property estimations, and explicit container data like initialization method, static factory method name, etc. A child bean definition acquires design information from a parent definition. The kid definition can abrogate a few qualities or add others, depending on the situation. Spring Bean definition inheritance steers clear of Java class inheritance, yet the inheritance idea is the same. You can characterize a parent bean definition as a layout, and other kid beans can acquire the necessary setup from the parent bean. When using XML-based design metadata, you show a kid bean definition by utilizing the parenting quality, indicating the parent bean as the worth of this characteristic. 

HelloWorld.java code:

package demo;

public class HelloWorld {
   private String message1;
   private String message2;
xml Code:
<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 = "demo.HelloWorld">
      <property name = "message1" value = "Hello World!"/>
      <property name = "message2" value = "Hello Universe!"/>
   </bean>

   <bean id ="helloEveryone" class = "demo.HelloEveryone" parent = "helloWorld">
      <property name = "message1" value = "Hello Everyone!"/>
      <property name = "message3" value = "Nice to Meet you!"/>
   </bean>
</beans>

   public void setMessage1(String message){
      this.message1 = message;
   }
   public void setMessage2(String message){
      this.message2 = message;
   }
   public void getMessage1(){
      System.out.println(message1);
   }
   public void getMessage2(){
      System.out.println(message2);
   }
}

HelloEveryone.java code:

package demo;

public class HelloEveryone {
   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(message1);
   }
   public void getMessage2(){
      System.out.println(message2);
   }
   public void getMessage3(){
      System.out.println(message3);
   }
}
MainApp.java code:
package demo;

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 = (HelloEveryone) context.getBean("helloEveryone");
      objB.getMessage1();
      objB.getMessage2();
      objB.getMessage3();
   }
}

Bean Definition Template 

You can make a Bean definition format, which other youngster bean definitions can utilize without investing much energy. While characterizing a Bean Definition Template, you ought not to indicate the class attribute, determine abstract characteristics, and indicate the theoretical trait with true worth.

<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 Universe!"/>
      <property name = "message3" value = "Hello Globe!"/>
   </bean>

   <bean id = "helloEveryone" class = "demo.HelloEveryone" parent = "beanTeamplate">
      <property name = "message1" value = "Hello Everyone!"/>
      <property name = "message3" value = "Nice to meet you!"/>
   </bean>
   
</beans>