在不改变目标方法基础上,对目标方法进行增强
    1.openfeign远程调用中其实就用到了远程调用

    1. /**
    2. * JDK动态代理流程
    3. * @param student
    4. */
    5. @Test
    6. public void ProxyTest1(Student student){
    7. StudentServiceImpl studentService=new StudentServiceImpl();
    8. IStudentService iStudentService = (IStudentService) Proxy.newProxyInstance(this.getClass().getClassLoader(), studentService.getClass().getInterfaces(),
    9. new InvocationHandler() {
    10. @Override
    11. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    12. if (method.getName().equals("addStudent")) {
    13. long start = System.currentTimeMillis();
    14. System.out.println("方法执行开始 = " + start);
    15. Object invoke = method.invoke(studentService, args);
    16. System.out.println("目标方法执行结束 = " + (System.currentTimeMillis() - start));
    17. //返回方法执行
    18. return invoke;
    19. }
    20. //其他方法不做代理增强,只对新增方法代理增强
    21. return method.invoke(studentService, args);
    22. }
    23. });
    24. //执行代理方法
    25. iStudentService.addStudent(student);
    26. }