Spring - Bean Definition Inheritance


A bean definition can contain a great deal of arrangement data, including constructor contentions, property estimations, and holder explicit data, for example, instatement strategy, static processing plant technique name, thus on.

A tyke bean definition acquires arrangement information from a parent definition. The youngster definition can abrogate a few qualities, or include others, as needed.

Spring Bean definition legacy has nothing to do with Java class legacy yet the legacy idea is same. You can characterize a parent bean definition as a layout and other youngster beans can acquire the required setup from the parent bean.

When you use XML-based design metadata, you demonstrate a youngster bean definition by utilizing the parent property, determining the parent bean as the estimation of this attribute.

Example

Let us have a working Eclipse IDE set up and find a way to make a Spring application −

Steps Description
1 Create a venture with a name SpringExample and make a bundle com.tutorialspoint under the src envelope in the made project.
2 Add required Spring libraries utilizing Add External JARs choice as clarified in the Spring Hello World Example chapter.
3 Create Java classes HelloWorld, HelloIndia and MainApp under the com.tutorialspoint package.
4 Create Beans design document Beans.xml under the src folder.
5 The last advance is to make the substance of all the Java records and Bean Configuration document and run the application as clarified below.

Following is the setup document Beans.xml where we characterized "helloWorld" bean which has two properties message1 and message2. Next "helloIndia" bean has been characterized as an offspring of "helloWorld" bean by utilizing parent trait. The tyke bean acquires message2 property as seems to be, and abrogates message1 property and presents one more property message3.

<?xml adaptation = "1.0" encoding = "UTF-8"?> 

<beans xmlns = "http://www.springframework.org/pattern/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/mapping/beans 

http://www.springframework.org/mapping/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 substance of HelloWorld.java record −

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 is the substance of HelloIndia.java document −

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

} 

}

Following is the substance of the MainApp.java record −

package com.tutorialspoint; 

import org.springframework.context.ApplicationContext; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class MainApp { 

public static void main(String[] args) { 

ApplicationContext setting = 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 are finished making the source and bean design records, let us run the application. In the event that all is well with your application, it will print the accompanying message −

World Message1 : Hello World! 

World Message2 : Hello Second World! 

India Message1 : Hello India! 

India Message2 : Hello Second World! 

India Message3 : Namaste India! 

If you saw here, we didn't pass message2 while making "helloIndia" bean, however it got passed due to Bean Definition Inheritance.

Bean Definition Template

You can make a Bean definition layout, which can be utilized by other tyke bean definitions without putting much exertion. While characterizing a Bean Definition Template, you ought not indicate the class property and ought to determine abstract trait and ought to determine the unique characteristic with an estimation of true as appeared in the accompanying code piece −

<?xml variant = "1.0" encoding = "UTF-8"?> 

<beans xmlns = "http://www.springframework.org/blueprint/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/blueprint/beans 

http://www.springframework.org/blueprint/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 can't be instantiated without anyone else in light of the fact that it is fragmented, and it is likewise expressly set apart as abstract. At the point when a definition is conceptual like this, it is usable just as an unadulterated format bean definition that fills in as a parent definition for youngster definitions.







© Javacodegeeks 2018 -2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3