Table of Contents
This is 11 of 16 parts of spring tutorial series: Inheritance in Spring.
Tutorial Content: Spring tutorial for beginners
- Introduction to spring framework
- Spring interview questions
- Dependency injection(ioc) in spring
- Spring XML based configuration example
- 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
Inheritance in Spring example
1.Person.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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
Create Employee.java under package org.arpit.javapostsforlearning.java..Copy following content into Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="personBean" class="org.arpit.javapostsforlearning.Person"> <property name="name" value="Arpit"/> </bean> <bean id="employeeBean" class="org.arpit.javapostsforlearning.Employee" parent="personBean"> <property name="employeeNumber" value="178230" /> </bean> </beans> |
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.
1 2 3 |
<bean id="employeeBean" class="org.arpit.javapostsforlearning.Employee" parent="id or name of parent bean"> |
4.InheritanceInSpringMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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()); } } |
5.Run it
1 2 3 4 |
Employee name:Arpit Employee number:178230 |
That’s all about Inheritance in Spring.
In next post,we will see spring applicationContext.