Inheritance in Spring

This is 11 of 16 parts of spring tutorial series: Inheritance in Spring.

Tutorial Content: Spring tutorial for beginners

In spring,inheritance is supported for reusing already written bean,so that beans can share common attributes and methods among them.

child bean will have all attributes and methods of parent bean,also child bean can override parent bean’s attributes or methods.

For configuring spring in your eclipse ide please refer  hello world example

Inheritance in Spring example

1.Person.java

This is simple pojo class having some attributes so here preson has name .
Create Person.java under package org.arpit.javapostsforlearning.Copy following content into Person.java.

2.Employee.java

This is also simple pojo class having one attribute called “employeeNumber”.

Create Employee.java under package org.arpit.javapostsforlearning.java..Copy following content into Employee.java

3.ApplicationContext.xml

Here We have declared two beans with corresponding ids.
1.Class Person with id as “PersonBean”
2.Class Employee with id as “EmployeeBean”
We have used  parent property of to show that Person is parent class of Employee.

4.InheritanceInSpringMain.java

This class contains main function.Create InheritanceInSpring.java under package org.arpit.javapostsforlearning.Copy following content into InheritanceInSpring.java
You can note here that we have used ClassPathXmlApplicationContext for getting bean here.There are various ways for getting beans.In hello world example we have used XmlBeanFactory for getting beans.

5.Run it

When you will run above application,you will get following as output.

That’s all about Inheritance in Spring.
In next post,we will see spring applicationContext.

Was this post helpful?

Leave a Reply

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