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.
package org.arpit.javapostsforlearning;

public class Person {

    String name;

    public String getName() {
        return name;
    }

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

}

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

package org.arpit.javapostsforlearning;

public class Employee extends Person{

    int employeeNumber;

    public int getEmployeeNumber() {
        return employeeNumber;
    }

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

}

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
package org.arpit.javapostsforlearning;

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

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.

5.Run it

When you will run above application,you will get following as output.
Employee name:Arpit
Employee number:178230

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

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.