工具类
package com.example.demo.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @Author: zhangjun
* @Date: 2021/5/21 20:45
*/
public class ProxyInvocationHandler2 implements InvocationHandler {
//要代理的对象
private Object target;
//初始化要代理的对象
public void setTarget(Object target) {
this.target = target;
}
//动态获得代理类
public Object getProxy() {
return Proxy.newProxyInstance(this.getClass().getClassLoader()
, target.getClass().getInterfaces()
, this);
}
//执行代理方法
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("相当于apo的before");
Object result = method.invoke(target, args);
System.out.println("相当于aop的after");
return result;
}
}