Monday, July 22, 2013

15-Writing-a-BeanPostProcessor

BeanPostProcessor are classes which tells the spring before and after initialization of beam some post processing need to do.
 For every bean post processor is called, it is a separate class, in order to make any class post processor you need to implement “BeanPostProcessor” interface and you need to configure in spring.xml.
Note that you can have destroy method it is only used while initialization.

DrawingApp.java
package org.yash.watertechsol;

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


public class DrawingApp {
      public static void main(String[] args){
            AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
            context.registerShutdownHook();
            Triangle triangle = (Triangle)context.getBean("triangle");
            triangle.draw();
      }
}

DisplayNameBeanPostProcessor.java
package org.yash.watertechsol;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class DisplayNameBeanPostProcessor implements BeanPostProcessor {

      @Override
      /*Input params:
       * String beanName: It is bean name eg:id="point"
       * Object beanObj: It is bean object eg:class="org.yash.watertechsol.Point"
       * */
      public Object postProcessAfterInitialization(Object beanObj, String beanName)
                  throws BeansException {
            System.out.println("In After Initialization. Bean name is " + beanName);
            return beanObj;
      }

      @Override
      public Object postProcessBeforeInitialization(Object beanObj, String beanName)
                  throws BeansException {
            System.out.println("In Before Initialization method. Bean name is " + beanName);
            return beanObj;
      }

}

spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
           
      <bean id="triangle" class="org.yash.watertechsol.Triangle" autowire="byName">
      </bean>
     
      <bean id ="pointA" class="org.yash.watertechsol.Point">
            <property name="x" value="0" />
            <property name="y" value="0" />
      </bean>
     
      <bean id ="pointB" class="org.yash.watertechsol.Point">
            <property name="x" value="-20" />
            <property name="y" value="0" />
      </bean>

      <bean id ="pointC" class="org.yash.watertechsol.Point">
            <property name="x" value="0" />
            <property name="y" value="20" />
      </bean>    

      <bean class="org.yash.watertechsol.DisplayNameBeanPostProcessor" />

</beans>


Triangle.java
package org.yash.watertechsol;


public class Triangle{
     
      private Point pointA;
      private Point pointB;
      private Point pointC;
     
      public Point getPointA() {
            return pointA;
      }

      public void setPointA(Point pointA) {
            this.pointA = pointA;
      }

      public Point getPointB() {
            return pointB;
      }

      public void setPointB(Point pointB) {
            this.pointB = pointB;
      }

      public Point getPointC() {
            return pointC;
      }

      public void setPointC(Point pointC) {
            this.pointC = pointC;
      }

      public void draw(){
            System.out.println("Point A = (" + getPointA().getX()+", "+getPointA().getY()+")");
            System.out.println("Point B = (" + getPointB().getX()+", "+getPointB().getY()+")");
            System.out.println("Point C = (" + getPointC().getX()+", "+getPointC().getY()+")");
      }

}

Point.java
package org.yash.watertechsol;

public class Point {
      private int x;
      private int y;
     
      public int getX() {
            return x;
      }
      public void setX(int x) {
            this.x = x;
      }
      public int getY() {
            return y;
      }
      public void setY(int y) {
            this.y = y;
      }
}

Output:
In Before Initialization method. Bean name is pointA
In After Initialization. Bean name is pointA
In Before Initialization method. Bean name is pointB
In After Initialization. Bean name is pointB
In Before Initialization method. Bean name is pointC
In After Initialization. Bean name is pointC
In Before Initialization method. Bean name is triangle
In After Initialization. Bean name is triangle
Point A = (0, 0)
Point B = (-20, 0)

Point C = (0, 20)

No comments:

Post a Comment