Spring Bean Definition Inheritance

A child bean definition is a bean definition that inherits configuration data from a parent definition. It is then able to override some values, or add others, as needed. When using XML-based configuration a child bean definition is indicated simply by using the ‘parent’ attribute, specifying the parent bean as the value of this attribute.

Following example demonstrates PropertyPlaceholderConfigurer using Annotation:

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

  <dependencies>
   	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring</artifactId>
		<version>2.5.6</version>
	</dependency>
  </dependencies>

1. POJO

Create Vehicle Class as a parent class

package com.kruders.bean;

public class Vehicle {
	private int tyreCount;

	public int getTyreCount() {
		return tyreCount;
	}

	public void setTyreCount(int tyreCount) {
		this.tyreCount = tyreCount;
	} 
}

Now create Car Class that extends Vehicle class

package com.kruders.bean;

public class Car extends Vehicle{
	private boolean airBag;

	public boolean isAirBag() {
		return airBag;
	}

	public void setAirBag(boolean airBag) {
		this.airBag = airBag;
	}
}

2. Configuration

Create Spring-Bean.xml and write the following code

<?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="vehicle" class="com.kruders.bean.Vehicle">
	   <property name="tyreCount" value="4" />
	</bean>
	
	<bean id="car" class="com.kruders.bean.Car" parent="vehicle">
		<property name="airBag" value="No" />
	</bean>
</beans>

A child bean definition can also inherit constructor argument values, property values and method overrides from the parent, with the option to add new values. If any init-method, destroy-method and/or static factory method settings are specified, they will override the corresponding parent settings.

The remaining settings will always be taken from the child definition: depends on, autowire mode, dependency check, singleton, scope, lazy init.

3. Run Program

Create Main.java class and run it as Java Application

package com.kruders.core;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.kruders.bean.Car;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Beans.xml");
		Car car = (Car) context.getBean("car");

		System.out.println("No. of Tyres : " + car.getTyreCount());
		System.out.println("Contains Airbag : " + car.isAirBag());
	}
}

When you run the above example you’ll get an output like:

No. of Tyres : 4
Contains Airbag : false

In the following example, we have explicitly marked the parent bean definition as abstract by using the abstract attribute. In the case that the parent definition does not specify a class, and so explicitly marking the parent bean definition as abstract is required.

<bean id="vehicle" abstract="true">
   <property name="tyreCount" value="4" />
</bean>

<bean id="car" class="com.kruders.bean.Car" parent="vehicle">
   <property name="airBag" value="No" />
</bean>

In the following example, you can see that car bean is overriding the “tyreCount” property.

<bean id="vehicle" abstract="true">
   <property name="tyreCount" value="2" />
</bean>

<bean id="car" class="com.kruders.bean.Car" parent="vehicle">
   <property name="tyreCount" value="4" />
   <property name="airBag" value="No" />
</bean>

The folder structure of the example is shown below in Figure 50.1

Figure 50.1 Figure 50.1

You can download the source code of this example here.


No comments yet.

Leave a Reply