Inheritance in Spring

This is 11 of 16 parts of tutorial series

Tutorial Content: Spring tutorial for beginners

    Introduction to spring framework Spring interview questions Dependency injection(ioc) in spring Spring hello world example in eclipse Spring java based configuaration Dependency injection via setter method in spring Dependency injection via constructor in spring Spring Bean scopes with examples Initializing collections in spring Beans Autowiring in spring Inheritance in Spring Spring ApplicationContext Spring lifetime callbacks BeanPostProcessors in Spring Annotation based Configuration in spring Spring AOP tutorial
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

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

package org.arpit.javapostsforlearning;

public class Person {

 String name;

 public String getName() {
  return name;

 public void setName(String name) { = name;

This is also simple pojo class having one attribute called "employeeNumber".
Create under package following content into

package org.arpit.javapostsforlearning;

public class Employee extends Person{
 int employeeNumber;

 public int getEmployeeNumber() {
  return employeeNumber;

 public void setEmployeeNumber(int employeeNumber) {
  this.employeeNumber = employeeNumber;

This class contains main function.Create under package org.arpit.javapostsforlearning.Copy following content into

package org.arpit.javapostsforlearning;

import org.springframework.context.ApplicationContext;

public class InheritenceInSpringMain {

 public static void main(String[] args) {
  ApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
  Employee emp=(Employee) appContext.getBean("employeeBean");
  System.out.println("Employee name:"+emp.getName());
  System.out.println("Employee number:"+emp.getEmployeeNumber());
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.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
 xmlns:xsi="" xmlns:aop=""

 <bean id="personBean" class="org.arpit.javapostsforlearning.Person">
  <property name="name" value="Arpit"/>
 <bean id="employeeBean" class="org.arpit.javapostsforlearning.Employee" parent="personBean">
  <property name="employeeNumber" value="178230" />

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.

<bean id="employeeBean" class="org.arpit.javapostsforlearning.Employee" parent="id or name of parent bean">

5.Run it

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

Employee name:Arpit
Employee number:178230

Source code:

In next post,we will see spring applicationContext.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012