Saturday, 25 August 2012

Inheritance in Spring

This is 9 of 13 parts of tutorial series

Tutorial Content:


Part-1:Introduction to spring framework Part-2:Dependency injection(ioc) in spring Part-3:Spring hello world example in eclipse Part-4:Dependency injection via setter method in spring Part-5:Dependency injection via constructor in spring Part-6:Spring Bean scopes with examples Part-7:Initializing collections in spring Part-8:Beans Autowiring in spring Part-9:Inheritance in Spring Part-10:Spring ApplicationContext Part-11:Spring lifetime callbacks Part-12:BeanPostProcessors in Spring Part-13:Annotation based Configuration in spring
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

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.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.

4.ApplicationContext.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: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.

<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.
 

Java tutorial for beginners Copyright © 2011 - |- Template created by O Pregador - |- Powered by Blogger Templates