1. import java.lang.reflect.InvocationHandler;
    2. import java.lang.reflect.Method;
    3. public class JdkProxy implements InvocationHandler {
    4. private Object target;
    5. public JdkProxy(Object target) {
    6. this.target = target;
    7. }
    8. @Override
    9. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    10. System.out.println("\r\n jdk代理之前");
    11. Object invoke = method.invoke(target, args);
    12. System.out.println("\r\n jdk代理之后");
    13. return invoke;
    14. }
    15. }
    16. public static void main(String[] args) {
    17. // System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
    18. OrderService orderService = new OrderService();
    19. JdkProxy proxy = new JdkProxy(orderService);
    20. Class<?> clazz = orderService.getClass();
    21. IOrderService service = (IOrderService) Proxy.newProxyInstance(clazz.getClassLoader(), clazz.getInterfaces(), proxy);
    22. service.doAdd(123456);
    23. }