Monday, July 22, 2013

35-Understanding-AOP-Proxies

We can implement our own proxy objects using  spring.

public class ShapeServiceProxy extends ShapeService{

      public Circle getCircle() {
            new LoggingAspect().loggingAdvice();
            return super.getCircle();
      }
     
}

public class FactoryService {

      public Object getBean(String beanType){
           
            if(beanType.equals("shapeServiceProxy")) return new ShapeServiceProxy();
            if(beanType.equals("circle")) return new Circle();
            if(beanType.equals("trianle")) return new Triangle();
            return null;
           
      }
}




AopMain.java
package org.yash.watertechsol;

import org.yash.watertechsol.service.FactoryService;
import org.yash.watertechsol.service.ShapeService;

public class AopMain {

      public static void main(String args[]){
            //ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
            //ShapeService shapeService = ctx.getBean("shapeService", ShapeService.class);
           
            FactoryService factoryService = new FactoryService();
            //ShapeService shapeService = (ShapeService) factoryService.getBean("shapeService");
            ShapeService shapeService = (ShapeService) factoryService.getBean("shapeServiceProxy");
            shapeService.getCircle();
      }
}


Circle.java
package org.yash.watertechsol.model;

public class Circle {
      private String name;

      public String getName() {
            return name;
      }

      public void setName(String name) {
            this.name = name;
            System.out.println("Circle Setter is called");
            throw(new RuntimeException());           
      }
     
      public String setNameandReturn(String name) {
            this.name = name;
            System.out.println("Circle Setter is called");
            return name;
      }    
}

Triangle.java
package org.yash.watertechsol.model;

public class Triangle {
      private String name;

      public void setName(String name) {
            this.name = name;
      }

      public String getName() {
            return name;
      }
}

ShapeService.java
package org.yash.watertechsol.service;

import org.yash.watertechsol.model.Circle;
import org.yash.watertechsol.model.Triangle;

public class ShapeService {
      private Triangle triangle;
      private Circle circle;

      public Circle getCircle() {
            System.out.println("Circle getter called");
            return circle;
      }
     
      public void setCircle(Circle circle) {
            this.circle = circle;
      }
     
      public Triangle getTriangle() {
            return triangle;
      }
      public void setTriangle(Triangle triangle) {
            this.triangle = triangle;
      }    

}

LoggingAspect.java
package org.yash.watertechsol.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;


@Aspect
public class LoggingAspect {

            public Object myAroundAdvice(ProceedingJoinPoint pjp){
                  Object returnValue = null;
                  try {
                        System.out.println("Before Advice");
                        returnValue = pjp.proceed();
                        System.out.println("After Returning");
                  } catch (Throwable e) {
                        System.out.println("AfterThrowing");
                  } // this is place where actual method execution happens
                  System.out.println("After Finally");
                  return returnValue;
            }

            public void loggingAdvice(){
                  System.out.println("Logging from the advice");
            }
                                   
}


FactoryService.java
package org.yash.watertechsol.service;

import org.yash.watertechsol.model.Circle;
import org.yash.watertechsol.model.Triangle;

public class FactoryService {

      public Object getBean(String beanType){
           
            if(beanType.equals("shapeServiceProxy")) return new ShapeServiceProxy();
            if(beanType.equals("circle")) return new Circle();
            if(beanType.equals("trianle")) return new Triangle();
            return null;
           
      }
}

ShapeServiceProxy.java
package org.yash.watertechsol.service;

import org.yash.watertechsol.aspect.LoggingAspect;
import org.yash.watertechsol.model.Circle;

public class ShapeServiceProxy extends ShapeService{

      public Circle getCircle() {
            new LoggingAspect().loggingAdvice();
            return super.getCircle();
      }
     
}

spring.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"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

      <aop:aspectj-autoproxy />

      <bean id="triangle" class="org.yash.watertechsol.model.Triangle">
            <property name="name" value="Triangle Name"></property>
      </bean>

      <bean id="circle" class="org.yash.watertechsol.model.Circle">
           
      </bean>

      <bean id="shapeService" class="org.yash.watertechsol.service.ShapeService" autowire="byName"/>
      <bean id="loggingAspect" class="org.yash.watertechsol.aspect.LoggingAspect"/>
     
      <aop:config>
            <aop:aspect id="loggingAspecta" ref ="loggingAspect">
                  <!--
                  <aop:pointcut id="allGetters" expression="execution(* get*())"/>
                  <aop:around method="myAroundAdvice" pointcut-ref="allGetters"/>
                   -->
                        <aop:around method="myAroundAdvice" pointcut="execution(* get*())"/>
            </aop:aspect>
      </aop:config>
     

</beans>

No comments:

Post a Comment