原文地址
@Pointcut(“execution(public * com.xxx.yyy.controller..(…))”)
public void pointcut(){
}
@Around("pointcut()")
public ApiResult doAround(ProceedingJoinPoint joinPoint) throws Throwable {
Object[] args = joinPoint.getArgs();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//1.获取目标类上的目标注解(可判断目标类是否存在该注解)
HasPermission annotationInClass = AnnotationUtils.findAnnotation(signature.getClass(), HasPermission.class);
//2.获取目标方法上的目标注解(可判断目标方法是否存在该注解)
HasPermission annotationInMethod = AnnotationUtils.findAnnotation(signature.getMethod(), HasPermission.class);
//ps:如果1.无法获取类上的注解时
//使用反射的方式
/*
* MethodSignature signature = (MethodSignature) joinPoint.getSignature();
* Class<?> tagClass = signatureInMethod.getDeclaringType();
* boolean annotation = tagClass.isAnnotationPresent(HasPermission.class);
* HasPermission annotationInClass=null;
* if(annotation){
* annotationInClass = tagClass.getAnnotation(HasPermission.class);
* }
*
*/
//....
//具体业务逻辑
//....
return (ApiResult) joinPoint.proceed(args);
}