Wednesday, April 24, 2019

Create Custom AOP

Q- How to create custom AOP in java ?



AbstractHandler.java
package com.shubh.example.aop;
import java.lang.reflect.InvocationHandler;

public abstract class AbstractHandler implements InvocationHandler {

 /** The target object. */

 private Object targetObject;

 /**
  * Sets the target object.
  * @param targetObject
  *  the new target object
  */
 public void setTargetObject(Object targetObject) {
  this.targetObject = targetObject;
 }

 /**
  * Gets the target object.
  * @return the target object
  */
 public Object getTargetObject() {
  return targetObject;
 }

}

AfterHandler.java

package com.shubh.example.aop;
import java.lang.reflect.Method;

public abstract class AfterHandler extends AbstractHandler {

 /**
  * Handles after the execution of method.
  * @param proxy the proxy
  * @param method the method
  * @param args the args
  */
 public abstract void afterHandle(Object proxy, Method method, Object[] args);

 /*
  * (non-Javadoc)
  * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
  * java.lang.reflect.Method, java.lang.Object[])
  */
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  Object result = method.invoke(getTargetObject(), args);
  afterHandle(proxy, method, args);
  return result;

 }

}

BeforeHandler.java

package com.shubh.example.aop;
import java.lang.reflect.Method;

public abstract class BeforeHandler extends AbstractHandler {

 /**
  * Handles before execution of actual method.
  * @param proxy the proxy
  * @param method the method
  * @param args the args
  */
 public abstract void beforeHandle(Object proxy, Method method, Object[] args);

 /*
  * (non-Javadoc)
  * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
  * java.lang.reflect.Method, java.lang.Object[])
  */
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  beforeHandle(proxy, method, args);
  return method.invoke(getTargetObject(), args);
 }
}


ProxyFactory.java 

package com.shubh.example.aop;
import java.lang.reflect.Proxy;
import java.util.List;
public class ProxyFactory {

 /**
  * Gets the proxy.
  * @param targetObject the target object
  * @param handlers  the handlers
  * @return the proxy
  */
 public static Object getProxy(Object targetObject, List<AbstractHandler> handlers) {
  Object proxyObject = null;
  if (handlers.size() > 0) {
   proxyObject = targetObject;
   for (int i = 0; i < handlers.size(); i++) {
    handlers.get(i).setTargetObject(proxyObject);
    proxyObject = Proxy.newProxyInstance(targetObject.getClass()
      .getClassLoader(),
      targetObject.getClass().getInterfaces(),
      handlers.get(i));
   }
   return proxyObject;
  } else {
   return targetObject;
  }
 }
}

AfterHandlerImpl.java

package com.shubh.example.aop.impl;
import java.lang.reflect.Method;
import com.shubh.example.aop.AfterHandler;

public class AfterHandlerImpl extends AfterHandler {

 @Override
 public void afterHandle(Object proxy, Method method, Object[] args) {
  System.out.println("Handling after actual method execution ........");
  System.out.println("Inside afterHandle - Method Name : " + method.getName());
  for (Object obj : args) {
   System.out.println("Inside afterHandle - Method argument : " + obj.toString());
  }
 }
}

BeforeHandlerImpl.java

package com.shubh.example.aop.impl;
import java.lang.reflect.Method;
import com.shubh.example.aop.BeforeHandler;

public class BeforeHandlerImpl extends BeforeHandler {

 /*
  * (non-Javadoc)
  * beforeHandle(java.lang.Object, java.lang.reflect.Method,
  * java.lang.Object[])
  */
 @Override
 public void beforeHandle(Object proxy, Method method, Object[] args) {
  System.out.println("Handling before actual method call ........");
  for (Object obj : args) {
   System.out.println("Inside beforeHandle - Method argument : " + obj.toString());
  }
 }
}
 
Calculator.java

package com.shubh.example.beans;

public interface Calculator {
 /**
  * Calculate.
  * @param a int a
  * @param b  int b
  * @return the int
  */
 public int calculate(int a, int b);
}
 
CalculatorImpl.java 
package com.shubh.example.beans;

public class CalculatorImpl implements Calculator {

 public int calculate(int a, int b) {
  System.out.println("**********Actual Method Call : CalculatorImpl **********");
  int sum = a + b;
  return sum;
 }
}

DynamicAopMain.java

package com.shubh.example.aop.app;
import java.util.ArrayList;
import java.util.List;
import com.shubh.example.aop.AbstractHandler;
import com.shubh.example.aop.AfterHandler;
import com.shubh.example.aop.BeforeHandler;
import com.shubh.example.aop.ProxyFactory;
import com.shubh.example.aop.impl.AfterHandlerImpl;
import com.shubh.example.aop.impl.BeforeHandlerImpl;
import com.shubh.example.beans.Calculator;
import com.shubh.example.beans.CalculatorImpl;

public class DynamicAopMain {

 public static void main(String[] args) {
  CalculatorImpl calcImpl = new CalculatorImpl();
  BeforeHandler before = new BeforeHandlerImpl();
  AfterHandler after = new AfterHandlerImpl();

  List handlers = new ArrayList();
  handlers.add(before);
  handlers.add(after);

  Calculator proxy = (Calculator) ProxyFactory.getProxy(calcImpl, handlers);
  int result = proxy.calculate(20, 10);
  System.out.println("FInal Result :::" + result);
 }
}










No comments:

Post a Comment