自定义的注解必须包含@Target(value = ElementType.XXX) 和 @Retention(value = RetentionPolicy.XXX)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Logs {
public String value();
}
然后获取指定注解:
private Log getAnnotation(ProceedingJoinPoint joinPoint) throws NoSuchMethodException {
// 获取类名
Class<?> targetClass = joinPoint.getTarget().getClass();
// 获取方法名
String methodName = joinPoint.getSignature().getName();
// 获取参数列表
Class<?>[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getParameterTypes();
// 获取方法
Method objMethod = targetClass.getMethod(methodName, parameterTypes);
// 获取方法的注解, 如果没有则返回null
return objMethod.getDeclaredAnnotation(Logs.class);
}