Monday, April 23, 2012

Writing a Spring BeanFactoryPostProcessor


we'll understand what a BeanPostProcessor is. We'll also write a BeanPostProcessor that prints a message upon initializing each and every bean in the Applicationcontext.xml




package com.venkat.core;


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


public class DisplayNameBeanPostProcessor implements BeanPostProcessor {


 @Override
 public Object postProcessAfterInitialization(Object bean, String beanName)
   throws BeansException {
  System.out.println("Bean After Method is invoked  "+beanName);
  return bean;
 }


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


}
-------------------------------------------------------------------------------------------------------------------------------------------------------
package com.venkat.core;


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




public class DrawingApp {


 /**
  * @param args
  */
 public static void main(String[] args) {
  AbstractApplicationContext factory=new ClassPathXmlApplicationContext("applicationContext.xml");
  factory.registerShutdownHook();
  Triangle t=(Triangle)factory.getBean("triangleBean");
  t.draw();


 }


}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
package com.venkat.core;


public class Point {


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



}
----------------------------------------------------------------------------------------------------------------------------------------------------------
package com.venkat.core;


import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;


public class Triangle implements InitializingBean,DisposableBean{


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() +")");
 }


 @Override
 public void destroy() throws Exception {
System.out.println("Destory Methood Called.........");  
 }


 @Override
 public void afterPropertiesSet() throws Exception {
System.out.println("Initilize method called.........");  
 }


 public void mydestroy()
 {
  System.out.println("my Destory method");
 }

 public void myInit()
 {
  System.out.println("My Init method.........");
 }
}
------------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans default-init-method="myInit" default-destroy-method="mydestroy"
 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="triangleBean" class="com.venkat.core.Triangle" >
  <property name="pointA" ref="zeroPoint"/>
  <property name="pointB" ref="point1"/> 
  <property name="pointC" ref="point2"/>
  </bean>
  
<bean id="zeroPoint" class="com.venkat.core.Point">
 <property name="x" value="0"/>
 <property name="y" value="0"/>
 </bean>
 <bean id="point1" class="com.venkat.core.Point">
 <property name="x" value="10"/>
 <property name="y" value="10"/>
 </bean>
 <bean id="point2" class="com.venkat.core.Point">
 <property name="x" value="0"/>
 <property name="y" value="10"/>
 </bean>
 <bean class="com.venkat.core.DisplayNameBeanPostProcessor">
 </bean>
</beans>.

No comments:

Post a Comment