在不改变目标方法基础上,对目标方法进行增强
1.openfeign远程调用中其实就用到了远程调用
/**
* JDK动态代理流程
* @param student
*/
@Test
public void ProxyTest1(Student student){
StudentServiceImpl studentService=new StudentServiceImpl();
IStudentService iStudentService = (IStudentService) Proxy.newProxyInstance(this.getClass().getClassLoader(), studentService.getClass().getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("addStudent")) {
long start = System.currentTimeMillis();
System.out.println("方法执行开始 = " + start);
Object invoke = method.invoke(studentService, args);
System.out.println("目标方法执行结束 = " + (System.currentTimeMillis() - start));
//返回方法执行
return invoke;
}
//其他方法不做代理增强,只对新增方法代理增强
return method.invoke(studentService, args);
}
});
//执行代理方法
iStudentService.addStudent(student);
}