In Spring, the inheritance is supported in bean configuration and it is quite useful for a bean sharing some common value or configurations. A child bean or inherited bean will inherit the bean configuration, properties and some attributes from the parent bean. In additional, the child beans are allow to override the inherited value.

Inheritance Example

Here’s a Customer class, let see how the inheritance work.

package com.mkyong.common;
 
public class Customer 
{
	private Person person;
	private int type;
	private String action;
	private String Country;
 
	//getter and setter methods
 
	@Override
	public String toString() {
		return "Customer [Country=" + Country + ", action=" + action
				+ ", person=" + person + ", type=" + type + "]";
	}
 
}

Bean configuration file

<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-2.5.xsd">
 
	<bean id="BaseCustomerMalaysia" class="com.mkyong.common.Customer">
		<property name="country" value="Malaysia" />
	</bean>
 
	<bean id="CustomerBean" parent="BaseCustomerMalaysia">
		<property name="person" ref="PersonBean" />
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>
 
	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong1" />
		<property name="address" value="address 1" />
		<property name="age" value="28" />
	</bean>
 
</beans>

Above is a ‘BaseCustomerMalaysia’ bean contains a ‘Malaysia’ value for country property, and the ‘CustomerBean’ bean inherited this value from parent (‘BaseCustomerMalaysia’).

Run it

package com.mkyong.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext context = 
    	  new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
 
    	Customer cust = (Customer)context.getBean("CustomerBean");
    	System.out.println(cust);
 
    }
}

output

Customer [Country=Malaysia, action=buy, 
person=Person [address=address 1, age=28, name=mkyong1], type=1]

The ‘CustomerBean’ bean just inherited the country property from its parent (‘BaseCustomerMalaysia’).

Inheritance with abstract

In above example, the ‘BaseCustomerMalaysia’ is still able to instantiate.

Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");

If you want to make this base bean as a template and not allow to instantiate it, you can add an abstract attribute in the <bean> element. For example

<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-2.5.xsd">
 
	<bean id="BaseCustomerMalaysia" class="com.mkyong.common.Customer" 
                 abstract="true">
 
		<property name="country" value="Malaysia" />
	</bean>
 
	<bean id="CustomerBean" parent="BaseCustomerMalaysia">
		<property name="person" ref="PersonBean" />
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>
 
	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong1" />
		<property name="address" value="address 1" />
		<property name="age" value="28" />
	</bean>
 
</beans>

Now, the ‘BaseCustomerMalaysia’ bean is a pure template for the inheritance only, if you try to instantiate it, you will encounter the following error message.

Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
org.springframework.beans.factory.BeanIsAbstractException: 
Error creating bean with name 'BaseCustomerMalaysia': 
Bean definition is abstract

Another Inheritance Example

The parent bean is not necessary to define the class attribute, often times, you may just need a common property for sharing. Here’s is an example

<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-2.5.xsd">
 
	<bean id="BaseCustomerMalaysia" abstract="true">
		<property name="country" value="Malaysia" />
	</bean>
 
	<bean id="CustomerBean" parent="BaseCustomerMalaysia" 
	    class="com.mkyong.common.Customer">
 
		<property name="person" ref="PersonBean" />
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>
 
	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong1" />
		<property name="address" value="address 1" />
		<property name="age" value="28" />
	</bean>
 
</beans>

In this case, the ‘BaseCustomerMalaysia’ bean is a pure template for sharing the country property only.

Overrride it

You can override the inherited value by specify the new value in the child bean. Let’s see this example

<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-2.5.xsd">
 
	<bean id="BaseCustomerMalaysia" class="com.mkyong.common.Customer" 
                abstract="true">
 
		<property name="country" value="Malaysia" />
	</bean>
 
	<bean id="CustomerBean" parent="BaseCustomerMalaysia">
	        <property name="country" value="Japan" />
		<property name="person" ref="PersonBean" />
		<property name="action" value="buy" />
		<property name="type" value="1" />
	</bean>
 
	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong1" />
		<property name="address" value="address 1" />
		<property name="age" value="28" />
	</bean>
 
</beans>

The ‘CustomerBean’ bean is just override the parent (‘BaseCustomerMalaysia’) country property, from ‘Malaysia’ to ‘Japan’.

Customer [Country=Japan, action=buy, 
person=Person [address=address 1, age=28, name=mkyong1], type=1]

Conclusion

The Spring bean configuration inheritance is very useful to avoid the repeated common value or configurations for multiple beans.

This article was posted in Spring category.

Related Posts