Bean Definition Inheritance

A bean definition can contain a lot of configuration information, including constructor arguments, property values, and container-specific information such as initialization method, static factory method name, and so on.

As we have lot of beans definition in xml file and these beans have a common value to be defined, then bean definition can be very useful.

We could apply bean definition inheritance with properties, list of properties and etc.

To inherit a parent bean inside a child, we are required to define the "parent" attribute into the child bean definition.

This attribute accepts the parent bean name as attribute value and inherits properties defined into the parent bean definition.

Example

package com.javasafari;

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) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String toString(){
	StringBuffer buffer = new StringBuffer();
	buffer.append("Name : " + this.name);
	buffer.append("Phone : " + this.phone);
	buffer.append("Email : " + this.email);
	buffer.append("City : " + this.city);
	buffer.append("Country : " + this.country);
	return buffer.toString();
	}

}

spring-beans.xml

<beans xmlns=
"http://www.springframework.org/schema/beans"
xmlns:context=
"http://www.springframework.org/schema/context"
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-3.2.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/
schema/context/spring-context-3.2.xsd">

<context:component-scan 
base-package="com.javasafari"/>
<bean id="personBean"  
class="com.javasafari.Person" abstract="true">
<property name="name" value="Ravi"/>
<property name="phone" value="678567"/>
<property name="email" value="ravi@gmail.com"/>
<property name="city" value="Delhi"/>
<property name="country" value="India"/>
</bean>
<bean id="personBeanSub1" parent="personBean" >
<property name="name" value="Mohit"/>
<property name="phone" value="989654"/>
<property name="email" value="mohit@gmail.com"/>
</bean>
<bean id="personBeanSub2" parent="personBean">
<property name="name" value="Rahul"/>
<property name="phone" value="112345"/>
<property name="email" value="rahul@yahoo.com"/>
</bean>
<bean id="personBeanSub3" parent="personBean"/>
</beans>

Loader.java

package com.javasafari

import org.springframework.context.support.
ClassPathXmlApplicationContext;

public class Loader {
public static void main (String args[]){
ClassPathXmlApplicationContext 	applicationContext =
 new ClassPathXmlApplicationContext
("com/javasafari/spring-beans.xml");
Person child1 = (Person)
applicationContext.getBean("personBeanSub1");
Person child2 = (Person)
applicationContext.getBean("personBeanSub2");
Person child3 = (Person)
applicationContext.getBean("personBeanSub3");
System.out.println(child1.toString());
System.out.println(child2.toString());
System.out.println(child3.toString());
applicationContext.close();
	}
}

Output

Name :Ravi 
Phone: 989654
Email: ravi@gmail.com City : Delhi 
Country : India
Name  : Mohit 
Phone : 989654
Email : mohit@gmail.com City  : Delhi
Country : India
Name  : Rahul
Phone : 112345 
Email : rahul@yahoo.com City  : Delhi
Country : India