Monday, June 24, 2013

Spring Bean inheritance for beginner

Inheritance:  Inheritance is a way of establishing IS A relationship.
When you want to create a new a class “B” and there is already one class “A” has existed that is having some method and properties those you want to implement in your class, then you can derive your class from the existing class so that you can reuse the functionality.

For Spring Intruduction and Spring Basic Example visit my previous posts below

Spring Intruduction  Spring Core

Definitions: The class that is derived from the class is called sub class (derived class or Child class or extended class). The class from which your class is derived is called super class (base class or parent class).

From the above definition class B is sub class and A is super class.




Let’s have a look at my spring bean configuration file.

Here I have configured my super class “Employee” with the id employee , to this bean I am passing only common value like company name and location because all employees working under my company having same values for these two properties.

Configured one more bean “Manager” with the id manager, I want make it sub class for my super class Employee to do this I added one attribute in my bean configuration i.e. parent=”employee”, this will inform to the Spring container this class is derived from the Employee super class so that I can reuse the properties and method from the Employee class instead of writing again and again (Boiler plat code).


Configured one more bean “Developer” with the Id developer, same configuration applicable as manager bean.

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:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

      <bean id="employee" class="com.lova.inheritance.Employee">
            <property name="company" value="TP" />
            <property name="location" value="Bangalore"/>
      </bean>
     
      <bean id="manager" class="com.lova.inheritance.Manager" parent="employee">
            <property name="empName" value="Durga" />
            <property name="empNo" value="1310" />
            <property name="salary" value="20000" />
      </bean>
      <bean id="developer" class="com.lova.inheritance.Developer" parent="employee">
            <property name="empName" value="Lova" />
            <property name="empNo" value="1311" />
            <property name="salary" value="15000" />
      </bean>
</beans>


Employee.java

package com.lova.inheritance;

/**
 * @author Lovababu
 *
 */
public class Employee {

      private String empNo;

      private String empName;

      private double salary;
     
      private String company;

      private String location;
     
      //setter and getter method, you can generate using IDE.

      public void displayEmpInfo()
      {
            System.out.println("[Employee: Name:" + empName + " ID:" +  empNo
                        + " Company:" + company + " Location:" + location);
      }
}


Manager.java

I haven’t defined any properties in this class, but still I am able to set some data in this Object using inheritance.  All properties and diplayEmpInfo() method will be inherited from the super class Employee. For manager my company is offering 15% hike calculateHike () method will calculate managers hike it will display the accumulated salary.

package com.lova.inheritance;


/**
 * @author Lovababu
 *
 */
public class Manager extends Employee{

      public void calculateHike()
      {
            double hike = getSalary()*(00.15);
            System.out.println("Offer Amount will be:"+(getSalary()+hike));
      }
}

Developer.java

For Developer my company is offering 10% hike calculateHike () method will calculate managers hike it will display the accumulated salary.

package com.lova.inheritance;

/**
 * @author Lovababu
 *
 */
public class Developer extends Employee
{
      public void calculateHike()
      {
            double hike = getSalary()*(00.10);
            System.out.println("Offer Amount will be:"+(getSalary()+hike));
      }
}

SpringBeanInheritanceTest.java

This test program gets the manager bean and developer bean from the context and invokes the corresponding classes method to calculate hike and super class method to display employee info.


package com.lova.inheritance.test;

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

import com.lova.inheritance.Developer;
import com.lova.inheritance.Manager;

/**
 * @author Lovababu
 *
 */
public class SpringBeanInheritanceTest {

      public static void main(String[] args)
      {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
           
            //working with manager
            Manager manager = (Manager) context.getBean("manager");
            manager.displayEmpInfo();
            manager.calculateHike();
            //working with developer
            System.out.println("**************************************************");
            Developer developer = (Developer) context.getBean("developer");
            developer.displayEmpInfo();
            developer.calculateHike();
           
      }
}


Required Libraries:

Ø       org.springframework.core-3.0.5.RELEASE.jar
Ø       org.springframework.context-3.0.5.RELEASE.jar
Ø       org.springframework.beans-3.0.5.RELEASE.jar
Ø       org.springframework.asm-3.0.5.RELEASE.jar
Ø       org.apache.log4j-1.2.15.jar
Ø       org.apache.commons.logging-1.1.1.jar
Ø       org.springframework.expression-3.0.5.RELEASE.jar

No comments:

Post a Comment

UA-41474183-1