原文来自:https://blog.csdn.net/frightingforambition/article/details/78842306
这里碰到了一个问题 ,执行的方法有注解,但是通过Annotation an = method.getAnnotation(UserChangeLog.class);
取值为null.
具体情况和此篇文章类似:示例
其实问题很简单,是代理对象和目标对象的问题,通过代理模式生成代理对象上的方法不会有注解,我们要取注解只能从目标对象中取。
错误写法:
Method method=((MethodSignature)point.getSignature()).getMethod();
//此处method获取的是代理对象(由代理模式生成的)的方法
Annotation an = method.getAnnotation(UserChangeLog.class);
//此处 an 为 null
正确写法:
Method realMethod = point.getTarget().getClass().getDeclaredMethod(signature.getName(),
method.getParameterTypes());
//此处realMethod是目标对象(原始的)的方法
Annotation an = method.getAnnotation(UserChangeLog.class);
//此处 an 不为null