Bean Definition Inheritance in Spring

In my previous post I have explained how to inherit collection values from the parent bean. Spring offers out of the box support on inheritance for the bean definitions in the XML configuration file. You can define any number of beans and extend them by using the parent  attribute in the bean element. Child bean can inherit all the values, properties, methods, etc. from the parent beans. If you define the same method in the child bean, it simply overrides them in the child bean.

If parent bean have the class definition, then child bean need not define classes, it will use the parent bean. If you want to mention class name for the child bean, then it must have the same properties defined in the parent bean. Because it will try to search for the properties in the parent bean.

The parent bean is declared as abstract in the configuration file, it indicates that Spring container will not create any instance for that bean. If you try to access that bean, it will throw exception. If you don’t use a class type for the parent bean, then declaring it as abstract is mandatory.

Look at the below example. This is very simple example to demonstrate how to use the feature, you could try our more advanced examples and post your comments.


public class Person {
	private String name;
	private String phone;
	private String email;
	private String city;
	private String country;
	public Person(){
		System.out.println("Initializing bean...");
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getPhone() {
		return phone;
	public void setPhone(String phone) { = phone;
	public String getEmail() {
		return email;
	public void setEmail(String email) { = email;
	public String getCity() {
		return city;
	public void setCity(String city) { = city;
	public String getCountry() {
		return country;
	public void setCountry(String country) { = country;
	public String toString(){
		StringBuffer buffer = new StringBuffer();
		buffer.append("Name : " +;
		buffer.append("Phone : " +;
		buffer.append("Email : " +;
		buffer.append("City : " +;
		buffer.append("Country : " +;
		return buffer.toString();



<beans xmlns=""

	<context:component-scan base-package=""/>
	<bean id="personBean"  class="" abstract="true">
		<property name="name" value="Vijay"/>
		<property name="phone" value="678567"/>
		<property name="email" value=""/>
		<property name="city" value="Bangalore"/>
		<property name="country" value="India"/>
	<bean id="personBeanSub1" parent="personBean" >
		<property name="name" value="Muthu"/>
		<property name="phone" value="986756"/>
		<property name="email" value=""/>
	<bean id="personBeanSub2" parent="personBean">
		<property name="name" value="Hari"/>
		<property name="phone" value="452345"/>
		<property name="email" value=""/>
	<bean id="personBeanSub3" parent="personBean"/>



public class SpringContextLoader {
	public static void main (String args[]){
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("javabeat/net/core/ioc/spring-beans-ioc.xml");
		Person child1 = (Person)applicationContext.getBean("personBeanSub1");
		Person child2 = (Person)applicationContext.getBean("personBeanSub2");
		Person child3 = (Person)applicationContext.getBean("personBeanSub3");

If you run the above example, the result will be:

Initializing bean...
Initializing bean...
Initializing bean...
Name : MuthuPhone : 986756Email : muthu@javabeat.netCity : BangaloreCountry : India
Name : HariPhone : 452345Email : hari@javabeat.netCity : BangaloreCountry : India
Name : VijayPhone : 678567Email : vijay@javabeat.netCity : BangaloreCountry : India

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!