Monday, July 22, 2013

16-Writing-a-BeanFactoryPostProcessor

Property place holder configure

DrawingApp.java
package org.yash.watertechsol;

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


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


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;
      }
}

MyBeanFactoryPostProcessor.java
package org.yash.watertechsol;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

      @Override
      public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
                  throws BeansException {
            System.out.println("My Bean factory post processor is called");  
      }

}

config.properties
pointA.pointX=0
pointA.pointY=10

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="${pointA.pointX}" />
            <property name="y" value="${pointA.pointY}" />
      </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.MyBeanFactoryPostProcessor" />
     
      <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <!-- relative path of file -->
            <property name="locations" value="config.properties"></property>
      </bean>

</beans>


No comments:

Post a Comment