Home  >  Spring Core

Spring Bean Definition Inheritance Example using Annotation and XML Configuration with Parent and Template with Abstract Attribute

By Arvind Rai, January 03, 2016
On this page we will learn spring bean definition inheritance example using annotation and XML configuration with parent and template with abstract Attribute. Child bean can override parent bean properties and add new one too. In XML configuration, a bean inheritance is accomplished by using parent attribute of bean tag. We need to assign parent bean id to parent attribute in child bean. We can also create template in XML which will be inherited by child bean. To create template bean, we have to create a bean using abstract attribute of bean tag without assigning class. We need to assign abstract attribute value as true. We cannot directly use this bean in application. We can only inherit its property in our child bean. This type of bean is a pure template bean. In case of annotation, java inheritance is enough. We accomplish spring bean definition inheritance with java inheritance only. For template bean, there is no abstract attribute of bean in java configuration. Here on this page we will provide complete example for each and every cases.

Spring Bean Definition Inheritance with XML Configuration using Parent Attribute

In XML configuration, we inherit bean definition using bean parent attribute. The classes configured in XML must be in super class and sub-class relation so that child bean should be compatible with parent bean. All the properties defined are overridden by child bean. We can override the properties of parent bean and can add new properties in the child bean.
app-conf-1.xml
<?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.xsd">

    <bean id="animal" class="com.concretepage.Animal" init-method="initA">
    	<property name="name" value="Hathi"/>
    	<property name="age" value="20"/>
    </bean>
    <bean id="elephant" class="com.concretepage.Elephant" parent="animal" init-method="initE">
    	<property name="age" value="30"/>
    	<property name="location" value="Varanasi"/>
    </bean>
</beans> 
For the example we have created a bean with name animal and another bean with the name elephant. The elephant bean is using parent attribute to inherit the bean properties of animal. We are also overriding parent bean properties and adding new property to child bean. Find the java classes.
Animal.java
package com.concretepage;
public class Animal {
	private String name;
	private Integer age;
	public void initA() {
		System.out.println("Inside initA()");
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
Elephant.java
package com.concretepage;
public class Elephant extends Animal {
	private String location;
	public void initE() {
		System.out.println("Inside initE()");
	}	
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
} 
Using ClassPathXmlApplicationContext we are running our XML configuration demo.
SpringDemo.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
	public static void main(String[] args) {
		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("app-conf-1.xml");
		Elephant elephant=(Elephant)context.getBean("elephant");
		System.out.println(elephant.getLocation());
		System.out.println(elephant.getName());
		System.out.println(elephant.getAge());
    	        context.registerShutdownHook();
	}
}  
In the output we find that the properties set in animal bean has been inherited in child bean elephant. Find the output.
Inside initA()
Inside initE()
Varanasi
Hathi
30 

Template Bean Definition with Abstract Attribute in XML Configuration

In XML configuration, we can also create a template bean. Template bean is created as abstract bean using abstract attribute of bean tag. Template bean cannot be accessed directly. It can only be inherited. In child bean we can override template bean properties and can also add new properties. In template bean, class attribute is not required. We need to use abstract attribute as abstract="true"
app-conf-2.xml
<?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.xsd">

    <bean id="template" abstract="true">
    	<property name="name" value="Hathi"/>
    	<property name="age" value="30"/>
    </bean>        
    <bean id="animal" class="com.concretepage.Animal" parent="template">
    	<property name="age" value="50"/>
    </bean>
    <bean id="elephant" class="com.concretepage.Elephant" parent="animal">
    	<property name="location" value="Delhi"/>
    </bean>
</beans> 
Run SpringDemo class changing the line as below
AbstractApplicationContext  context = new ClassPathXmlApplicationContext("app-conf-2.xml");
 
And find the output
Delhi
Hathi
50 

Spring Bean Definition Inheritance Example using Annotation

In case of spring bean definition inheritance using annotation, java inheritance is enough. There is no way to create abstract bean in java configuration. To achieve same functionality of XML based bean definition into java configuration, we can do as follows.
Find the java configuration file.
AppConfig.java
package com.concretepage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
	@Bean
	public Elephant elephant() {
		Elephant elephant = new Elephant();
		initAnimal(elephant);
		elephant.setLocation("Delhi");
		return elephant;
	}
	private void initAnimal(Animal animal) {
		animal.setName("Hathi");
		animal.setAge(30);
	}
} 
Find the class to run the example.
SpringDemo.java
package com.concretepage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringDemo {
	public static void main(String[] args) {
	    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	    ctx.register(AppConfig.class);
	    ctx.refresh();
	    Elephant elephant = ctx.getBean(Elephant.class);
	    System.out.println(elephant.getName());
	    System.out.println(elephant.getLocation());
	    System.out.println(elephant.getAge());
    	    ctx.registerShutdownHook();
	}
}  
Find the output
Hathi
Delhi
30 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us