1.接口类
package com.hxzy.aop;
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.接口的实现类
package com.hxzy.aop;
public class ArithmeticCalculateImpl 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.接口实现类,包含输出日志功能
package com.hxzy.aop;
//ArithmeticCalculateImpl
public class ArithmeticCalculateLoggingImpl 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 " + 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"
+ " ends 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 ends with " + result);
return result;
}
@Override
public int div(int i, int j) {
System.out.println("package com.hxzy.aop;
//ArithmeticCalculateImpl
public class ArithmeticCalculateLoggingImpl 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 " + 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"
+ " ends 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 ends 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 ends with " + result);
return result;
}
}
4.直接使用3中的类运行程序,正确。但若是希望程序中输出日志统一修改为XXX The method xx begins with [" + i + ", " + j + "]"); 就需要将各个方法均修改一遍。
所以不合适
这就引出了动态代理的概念
package com.hxzy.aop;
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 h = new InvocationHandler() {
//proxy 正在返回的那个代理对象,一般情况下,在invoke方法中都不使用该对象,否则会引发死循环,java.lang.StackOverflowError
//method 正在被调用的方法
//args : 调用方法时,传入的参数
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
//此处不可以使用proxy.tostring方法,否则会引发死循环,java.lang.StackOverflowError
// System.out.println(proxy.toString());
// System.out.println("invoke ...");
String methodName = method.getName();
//日志
System.out.println("The method " + methodName + " begin with " + Arrays.asList(args));
//执行方法
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;
}
}
5.测试类
package com.hxzy.aop;
public class TestMain {
public static void main(String[] args) {
// ArithmeticCalculator arithmetic = new ArithmeticCalculateLoggingImpl();
//调用无日志类
ArithmeticCalculator target = new ArithmeticCalculateImpl();
ArithmeticCalculator proxy = new ArithmeticCalculatorLoggingProxy(target).getLoggingProxy();
int result = proxy.add(1, 2);
System.out.println("reuslt :" + result);
result = proxy.div(4, 2);
System.out.println("reuslt :" + result);
}
}