首页 > 代码库 > Spring学习笔记-AOP前传之动态代理
Spring学习笔记-AOP前传之动态代理
假设有如下需求:
写一个计算器类,里面包含加减乘除四个方法。在每个方法开始前打印出该方法开始的消息,在每个方法结束前打印出该方法结束的消息和计算的结果。
普通方法,先写一个借口,然后在接口里实现四个方法。在每个方法里加上要打印的语句。实现代码如下。
ArithmeticCalculator接口
package com.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); }
ArithmeticCalculatorLoggingImpl.java 实现上面的接口
package com.spring.aop.helloworld; public class ArithmeticCalculatorLoggingImpl 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 end with " + result); 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 end with " + result); 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 end with " + result); 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 end with " + result); return result; } }
Main.java
ArithmeticCalculator arithmeticCalculator = new ArithmeticCalculatorLoggingImpl(); arithmeticCalculator.add(1, 5); System.out.println("----------"); arithmeticCalculator.sub(5, 3); System.out.println("----------"); arithmeticCalculator.mul(3, 7); System.out.println("----------"); arithmeticCalculator.div(9, 3);
程序运行结果:
The method add begins with [1, 5]
The method add end with 6
----------
The method sub begins with [5, 3]
The method sub end with 2
----------
The method mul begins with [3, 7]
The method mul end with 21
----------
The method div begins with [9, 3]
The method div end with 3
可见,上面的代码中间存在这大量相似的代码。而面向对象编程又不能很好地解决这个问题,下面采用动态代理的方法来解决上面的问题。
接口不变。写一个实现类ArithmeticCalculatorImpl.java 这个实现类只关注业务,没有需要打印的内容
package com.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; } }
ArithmeticCaculatorLogginProxy.java
package com.spring.aop.helloworld; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.Arrays; public class ArithmeticCaculatorLogginProxy { //要代理的对象 private ArithmeticCalculator target; public ArithmeticCaculatorLogginProxy(ArithmeticCalculator target){ this.target = target; } public ArithmeticCalculator getLoggingProxy() { ArithmeticCalculator proxy = null; //代理对象由哪一个类加载器负责加载 ClassLoader loader = target.getClass().getClassLoader(); //代理对象的类型,即其中有哪些方法 Class[] interfaces = new Class[]{ArithmeticCalculator.class}; //当调用代理对象其中的方法时,该执行的代码 InvocationHandler h = new InvocationHandler() { /** * proxy:正在返回的代理对象,一般情况下,在invoke方法中都不适用该对象 * method:正在被调用的方法 * args:调用方法时,传入的参数 */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //下面这句执行的时候又会调用invoke方法,所以会出现死循环导致内存溢出 //System.out.println(proxy.toString()); String methodName = method.getName(); //日志 System.out.println("The method " + methodName + " begins with" + Arrays.asList(args)); System.out.println("Invoke..."); //执行方法 Object result = method.invoke(target, args); //日志 System.out.println("The method" + methodName + " ends with " + result); return result; } }; proxy = (ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h); return proxy; } }
main方法
ArithmeticCalculator target = new ArithmeticCalculatorImpl(); ArithmeticCalculator proxy = new ArithmeticCaculatorLogginProxy(target).getLoggingProxy(); proxy.add(1, 5); System.out.println("----------"); proxy.sub(5, 3); System.out.println("----------"); proxy.mul(3, 7); System.out.println("----------"); proxy.div(9, 3);
程序运行结果 :
The method add begins with[1, 5]
Invoke...
The methodadd ends with 6
----------
The method sub begins with[5, 3]
Invoke...
The methodsub ends with 2
----------
The method mul begins with[3, 7]
Invoke...
The methodmul ends with 21
----------
The method div begins with[9, 3]
Invoke...
The methoddiv ends with 3
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1557431
Spring学习笔记-AOP前传之动态代理