1.ArithmeticCalculator.java
package com.huanlgiusong.spring.aop.hello;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);}2.ArithmeticCalculatorImpl.java
package com.huanlgiusong.spring.aop.hello;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; }}3.ArithmeticCalculatorLoggingProxy.java
package com.huanlgiusong.spring.aop.hello;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.ArrayList;import java.util.Arrays;import org.aopalliance.intercept.Invocation;/** * 动态代理 * @author Administrator * */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 h=new InvocationHandler() { /** * proxy正在返回的那个代理对象 一般情况下在invoke方法中不适用对象 * method:正在被调用的方法 * args:正在被调用方式时传入的参数 */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("invoke。。。"); //日志 System.out.println("这个方法是"+method.getName()); System.out.println("这个方法参数是:"+Arrays.asList(args)); //执行方法 Object result=method.invoke(target, args); return result; } }; proxy=(ArithmeticCalculator) Proxy.newProxyInstance(loader, interfaces, h); return proxy; } }4.ArithmeticImpl.java
package com.huanlgiusong.spring.aop.hello;public class ArithmeticImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { System.out.println("这个方法是加法运算 <开始> " + i + "+" + j); int result = i + j; System.out.println("这个方法是加法运算 <结束> " + i + "+" + j + "=" + result); return result; } @Override public int sub(int i, int j) { System.out.println("这个方法是-法运算 <开始> " + i + "+" + j); int result = i - j; System.out.println("这个方法是-法运算 <结束> " + i + "-" + j + "=" + result); return result; } @Override public int mul(int i, int j) { System.out.println("这个方法是*法运算 <开始> " + i + "+" + j); int result = i * j; System.out.println("这个方法是*法运算 <结束> " + i + "*" + j + "=" + result); return result; } @Override public int div(int i, int j) { System.out.println("这个方法是/法运算 <开始> " + i + "+" + j); int result = i / j; System.out.println("这个方法是/法运算 <结束> " + i + "/" + j + "=" + result); return result; }} 结束> 开始> 结束> 开始> 结束> 开始> 结束> 开始>5.TestArithmetic.java
package com.huanlgiusong.spring.aop.hello;import org.junit.Test;public class TestArithmetic { @Test public void test1() { int result = 0; ArithmeticCalculator arithmeticCalculator = null; arithmeticCalculator = new ArithmeticCalculatorImpl(); result = arithmeticCalculator.add(1, 1); System.out.println("result>>" + result); result = arithmeticCalculator.sub(1, 1); System.out.println("result>>" + result); result = arithmeticCalculator.div(1, 1); System.out.println("result>>" + result); result = arithmeticCalculator.mul(1, 1); System.out.println("result>>" + result); } @Test public void test2(){ ArithmeticCalculator target=new ArithmeticCalculatorImpl(); ArithmeticCalculator proxy=new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy(); int result = proxy.add(1, 2); System.out.println("result>>" + result); result = proxy.sub(1, 3); System.out.println("result>>" + result); result = proxy.div(1, 4); System.out.println("result>>" + result); result = proxy.mul(1, 5); System.out.println("result>>" + result); }}