Tutorials

Inherit Spring Bean


Inherit Spring Bean


Spring lets us inherit property of other bean by allowing us to establish inheritance relationship.

Bean inheritance is achieved by applying parent attribute in the bean element declared in XML configuration file.

 

This is how we can establish inheritance relationship adding 'parent' attribute.

<bean id="studentBean" class="Student">  
<!-- more configurations -->
</bean>  
    
<bean id="e2" class="Subject" parent="studentBean ">  
<!-- more configurations -->
</bean>  
</beans>

Spring Bean Inheritance Example

An example of a bean inheriting property of parent bean is shown below:

 

Java POJO - Student.java

public class Student {
	private String name;
	private String stream;
	private Subjects subjects; //dependent object	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStream() {
		return stream;
	}

	public void setStream(String stream) {
		this.stream = stream;
	}

	public Subjects getSubjects() {
		return subjects;
	}

	public void setSubjects(Subjects subjects) {
		this.subjects = subjects;
	}

	//display details
	public void displayStudentInfo(){
		System.out.println("Name :"+ name + "    "+ "Stream :"+ " " +stream);
		System.out.println("Subject details: "+ subjects);
	}

}

Java POJO - Subjects.java


public class Subjects {

	private String subjectName;
	private String subjectID;
	
	public String getSubjectName() {
		return subjectName;
	}

	public void setSubjectName(String subjectName) {
		this.subjectName = subjectName;
	}

	public String getSubjectID() {
		return subjectID;
	}

	public void setSubjectID(String subjectID) {
		this.subjectID = subjectID;
	}

	//overriding toString()
	public String toString(){
		return subjectName+ " "+subjectID;
	}
}

XML Configuration - appContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<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="mukeshBean" class="Student">
<property name="name" value="Mukesh Tripathi"></property>
<property name="stream" value="ECE"></property>
</bean>

<bean id="studentBean" class="Student" parent="mukeshBean">  
<property name="subjects" ref="subjectBean"></property>
</bean>

<bean id="subjectBean" class="Subjects">  
<property name="subjectName" value="Data Structure"></property>
<property name="subjectID" value="DS101"></property>
</bean>

</beans>

Test Class - TestSpringClass.java

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

public class TestSpringClass {

	public static void main(String[] args) {		
		
		ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");
		
		Student student = (Student) context.getBean("studentBean");
		student.displayStudentInfo();
	}

}

OUTPUT

Name :Mukesh Tripathi    Stream : ECE
Subject details: Data Structure DS101

 

Its your task to write same code using constructor injection. This will be a good practice for you.

 



Please Share this page
Views : 83
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!