logo

Inheriting Bean In Spring


Show

The term inheritance means that we are getting the property from the parents. The same definition is being used in the Spring framework of Java. In Spring, the properties of the base class can be inherited by the child class, which also includes configuration information, constructor arguments, property values, container-specific information, and a lot of other key values. So the relationship between the parent and child bean is made by using the parent attribute of bean.

Here, we will give a simple example of bean inheritance.

Employee.Java

The Employee class includes three properties, three constructors, and the show() method to display the output values.

package com.intellinuts;  

public class Employee {  
private int id;  
private String name;  
private Address address;  
public Employee() {}  

public Employee(int id, String name) {  
    super();  
    this.id = id;  
    this.name = name;  
}  

public Employee(int id, String name, Address address) {  
    super();  
    this.id = id;  
    this.name = name;  
    this.address = address;  
}  

void show(){  
    System.out.println(id+" "+name);  
    System.out.println(address);  
}  
 
}  

Address.Java

Following is the code of the Address.java class.

package com.intellinuts;  

public class Address {  
private String addressLine1,city,state,country;  

public Address(String addressLine1, String city, String state, String country) {  
    super();  
    this.addressLine1 = addressLine1;  
    this.city = city;  
    this.state = state;  
    this.country = country;  
}  

public String toString(){  
    return addressLine1+" "+city+" "+state+" "+country;  

}  

}  

XML configuration will be as follows:

  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="e1" class="com.intellinuts.Employee">  
<constructor-arg value="101"></constructor-arg>  
<constructor-arg value="Sachin"></constructor-arg>  
</bean>  

<bean id="address1" class="com.intellinuts.Address">  
<constructor-arg value="21,Lohianagar"></constructor-arg>  
<constructor-arg value="Ghaziabad"></constructor-arg>  
<constructor-arg value="UP"></constructor-arg>  
<constructor-arg value="India"></constructor-arg>  
</bean>  

<bean id="e2" class="com.intellinuts.Employee" parent="e1">  
<constructor-arg ref="address1"></constructor-arg>  
</bean>  

</beans>

Test.Java

The Test class will give the output after getting the bean from the applicationContext.xml file and calling the show() method.

package com.intellinuts;  

import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.ClassPathResource;  
import org.springframework.core.io.Resource;  

public class Test {  
public static void main(String[] args) {  
    Resource r=new ClassPathResource("applicationContext.xml");  
    BeanFactory factory=new XmlBeanFactory(r);  
    Employee e1=(Employee)factory.getBean("e2");  
    e1.show();  
}  

}  

The output will be:

Employee [id= 101, name= Sachin, Address{ 21 Lohianagar, city= Ghaziabad, state= UP, Country= India}]