package com.atguigu.Aop; public class CalculatorLogImpl implements Calculator{ @Override public int add(int i, int j) { System.out.println("[日志] add 方法开始了,参数是:" + i + "," + j); int result = i + j; System.out.println("方法内部 result = " + result); System.out.println("[日志] add 方法结束了,结果是:" + result); return result; } @Override public int sub(int i, int j) { System.out.println("[日志] sub 方法开始了,参数是:" + i + "," + j); int result = i - j; System.out.println("方法内部 result = " + result); System.out.println("[日志] sub 方法结束了,结果是:" + result); return result; } @Override public int mul(int i, int j) { System.out.println("[日志] mul 方法开始了,参数是:" + i + "," + j); int result = i * j; System.out.println("方法内部 result = " + result); System.out.println("[日志] mul 方法结束了,结果是:" + result); return result; } @Override public int div(int i, int j) { System.out.println("[日志] div 方法开始了,参数是:" + i + "," + j); int result = i / j; System.out.println("方法内部 result = " + result); System.out.println("[日志] div 方法结束了,结果是:" + result); return result; } }
这是 使用静态代理前
package com.atguigu.Aop; public class CalculatorStaticProxy implements Calculator{ //把被代理的对象传进来 private Calculator calculator; public CalculatorStaticProxy(Calculator calculator) { this.calculator = calculator; } @Override public int add(int i, int j) { //输出日志 System.out.println("[日志] sub 方法开始了,参数是:" + i + "," + j); //调用目标对象的核心方法 int result = calculator.add(i,j); //输出日志 System.out.println("[日志] sub 方法结束了,结果是:" + result); return result; } @Override public int sub(int i, int j) { return 0; } @Override public int mul(int i, int j) { return 0; } @Override public int div(int i, int j) { return 0; } }
这个是使用静态代理后