Inheritance of Spring Beans

 Inheritance of Spring Beans

We have five files in our Spring application:

MainApp.java

package com.linaittech2;

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

Mirek objectA = (Mirek) context.getBean(“mirek”);
objectA.getMessage1();

Murek objectB = (Murek) context.getBean(“murek”);
objectB.getMessage1();
objectB.getMessage2();

Burek objectC = (Burek) context.getBean(“burek”);
objectC.getMessage1();
objectC.getMessage2();
objectC.getMessage3();

}

}

Mirek.java

package com.linaittech2;

public class Mirek {

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(“Message1 from Mirek : ” + message1);
}

public void getMessage2() {
System.out.println(“Message2 from Mirek : ” + message2);
}

}

Murek.java

package com.linaittech2;

public class Murek {

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 from Murek : ” + message1);
}

public void getMessage2(){
System.out.println(“Message2 from Murek : ” + message2);
}

public void getMessage3() {
System.out.println(“Message3 from Murek : ” + message3);
}

}

Burek.java

package com.linaittech2;

public class Burek {

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 from Burek : ” + message1);
}

public void getMessage2(){

System.out.println(“Message2 from Burek : ” + message2);
}

public void getMessage3(){
System.out.println(“Message3 from Burek : ” + message3);
}

}

Beans.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”&gt;

<bean id=”mirek” parent=”murek”>
<property name=”message1″ value=”mureczek!”/>
</bean>

<bean id=”murek”>
<property name=”message1″ value=”burek!”/>
<property name=”message2″ value=”burki!”/>
</bean>

<bean id=”burek” parent=”murek”>
<property name=”message1″ value=”murek!”/>
<property name=”message3″ value=”murki!”/>
</bean>

</beans>

Output:

gru 03, 2013 2:04:23 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5bcf3a: startup date [Tue Dec 03 14:04:23 CET 2013]; root of context hierarchy
gru 03, 2013 2:04:24 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
gru 03, 2013 2:04:25 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@7fb9e2: defining beans [mirek,murek,burek]; root of factory hierarchy
Message1 from Mirek : mureczek!
Message1 from Murek : burek!
Message2 from Murek : burki!
Message1 from Burek : murek!
Message2 from Burek : burki!
Message3 from Burek : murki!

The output presents us the mechanism of inheritance. It’s somewhat different than in ordinary Java classes, because the Beans.xml file is very important in this process, but the idea is the same: the child bean can override or add values from the parent one.

You can Beans.xml write in other way. We can make a Bean Definition Template for your application. And child beans can use it. In the Bean Definition Template, there’s no class attribute, but an abstract attribute with its value set to “true”.

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”&gt;

<bean id=”beanDefTemplate” abstract=”true”>
<property name=”message1″ value=”Mirek!”/>
<property name=”message2″ value=”Burek!”/>
<property name=”message3″ value=”Murek!”/>
</bean>

<bean id=”burek” parent=”beanDefTemplate”>
<property name=”message1″ value=”Mirek!”/>
<property name=”message3″ value=”Murek!”/>
</bean>

</beans>

Bean Definition Template is simply an abstract bean. It is similar to an abstract class in Java – it can’t be instantiated, only child beans can use data.

Leave a comment