import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class JdkProxy implements InvocationHandler {
private Object target;
public JdkProxy(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("\r\n jdk代理之前");
Object invoke = method.invoke(target, args);
System.out.println("\r\n jdk代理之后");
return invoke;
}
}
public static void main(String[] args) {
// System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
OrderService orderService = new OrderService();
JdkProxy proxy = new JdkProxy(orderService);
Class<?> clazz = orderService.getClass();
IOrderService service = (IOrderService) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), proxy);
service.doAdd(123456);
}