标签:
为什么需要AOP?
先来看一段代码:
package com.cn.spring.aop.helloworld; //加减乘除的接口类 public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); }
package com.cn.spring.aop.helloworld; //实现类 public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { int result = i + j; return result; } @Override public int sub(int i, int j) { int result = i - j; return result; } @Override public int mul(int i, int j) { int result = i * j; return result; } @Override public int div(int i, int j) { int result = i / j; return result; } }
现在问题来了:
现在需要在程序执行期间追踪正在发生的活动?
代码如下:
package com.cn.spring.aop.helloworld; //实现类 public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { System.out.println("The method add begins with[" + i + "," + j + "]"); int result = i + j; System.out.println("The method add ends with[" + i + "," + j + "]"); return result; } @Override public int sub(int i, int j) { System.out.println("The method sub begins with[" + i + "," + j + "]"); int result = i - j; System.out.println("The method sub ends with[" + i + "," + j + "]"); return result; } @Override public int mul(int i, int j) { System.out.println("The method mul begins with[" + i + "," + j + "]"); int result = i * j; System.out.println("The method mul ends with[" + i + "," + j + "]"); return result; } @Override public int div(int i, int j) { System.out.println("The method div begins with[" + i + "," + j + "]"); int result = i / j; System.out.println("The method div ends with[" + i + "," + j + "]"); return result; } }
如果还希望计算器的加减乘除只能处理整数的运算呢,又需要在ArithmeticCalculatorImpl 实现类中添加代码进行验证,每添加一个需求,都要去添加类似的代码。
使得代码冗余且维护性低。
那如何用AOP来解决呢?
AOP是使用代理设计模式的原理:使用一个代理将对象包装起来,然后用该代理对象取代原始对象。任何对原始对象的调用都要通过代理。代理对象决定是否以及何时将方法调用转到原始对象上。
先来看下代理是如何来实现的:
package com.cn.spring.aop.helloworld; //加减乘除的接口类 public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); }
package com.cn.spring.aop.helloworld; //实现类 public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { int result = i + j; return result; } @Override public int sub(int i, int j) { int result = i - j; return result; } @Override public int mul(int i, int j) { int result = i * j; return result; } @Override public int div(int i, int j) { int result = i / j; return result; } }
package com.cn.spring.aop.helloworld; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Arrays; public class ArithmeticCalculatorLoggingProxy { //要代理的对象 private ArithmeticCalculator target; public ArithmeticCalculatorLoggingProxy(ArithmeticCalculator target) { this.target = target; } public ArithmeticCalculator getLoggingProxy() { ArithmeticCalculator proxy = null; //代理对象由哪一个类加载器负责加载 ClassLoader loader = target.getClass().getClassLoader(); //代理对象的类型,即其中有哪些方法 Class[] interfaces = new Class[]{ArithmeticCalculator.class}; InvocationHandler handler = new InvocationHandler() { /** * * @param proxy 正在返回的那个代理对象,一般情况下,在invoke方法中都不使用该对象 * @param method 正在被调用的方法 * @param args 调用方法时,传入的参数 * @return * @throws Throwable */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); System.out.println("The methed " + methodName + "begins with " + Arrays.asList(args)); Object result = method.invoke(target, args); System.out.println("The methed " + methodName + "end with " + Arrays.asList(args)); return result; } }; proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, handler); return proxy; } }
package com.cn.spring.aop.helloworld; /** * Created by jecyhw on 2015/6/20. */ public class Main { public static void main(String[] args) { ArithmeticCalculator target = new ArithmeticCalculatorImpl(); ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy(); int result = proxy.add(1, 2); System.out.println("-->" + result); result = proxy.sub(1, 2); System.out.println("-->" + result); result = proxy.mul(1, 2); System.out.println("-->" + result); result = proxy.div(1, 2); System.out.println("-->" + result); } }
Spring AOP基础:
标签:
原文地址:http://www.cnblogs.com/jecyhw/p/4590362.html