Spring AOP 和 拦截器 获取类上与方法上的注解

方法1:

 https://blog.csdn.net/qq_37778018/article/details/125326847

 

在做一个跨过目标注解的鉴权功能时,想到了AOP与拦截器两种方式,其中 @HasPermission 是我自定义的注解,以下分别为AOP与拦截器获取访问目标类与方法上的注解的方法。由于我的系统在拦截器上配置了拦截过程,

所以我选的是拦截器的方式,读者可根据自己的需求来。

一、Spring AOP
先通过ProceedingJoinPoint对象的 joinPoint.getSignature()方法获取到 Signature 的对象并强制类型转换为一个MethodSignature对象,通过 signature.getClass()方法获取到一个 Class 对象,

最后通过 AnnotationUtils.findAnnotation()方法获取目标类上的目标注解;同理,通过 signature.getMethod()方法获取到一个 Method 对象,最后通过 AnnotationUtils.findAnnotation()方法获取目标方法上的目标注解。

 

@Aspect
@Component
public class PermissionAop {
 
    @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);
    }
 
}

 

二、拦截器 

将 handler 强制转换为 HandlerMetho 对象,再通过 HandlerMethod 对象的handlerMethod.getBeanType() 方法获取到 Bean 对象,最后通过 AnnotationUtils.findAnnotation()方法获取目标类上的目标注解;
同理,通过handlerMethod.getMethod() 方法获取到 Method 对象,最后通过 AnnotationUtils.findAnnotation()方法获取目标方法上的目标注解。 

 

@Component
public class PermissionInterceptor implements HandlerInterceptor {
 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        //1.获取目标类上的目标注解(可判断目标类是否存在该注解)
        HasPermission annotationInClass = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), HasPermission.class);
        //2.获取目标方法上的目标注解(可判断目标方法是否存在该注解)
        HasPermission annotationInMethod = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), HasPermission.class);
 
        //....
        //..业务逻辑代码
        //..
  }
}

 

 

方法2:

https://blog.csdn.net/weixin_43702146/article/details/126871811

 

AOP处理类上或者方法上面的自定义或指定注解

1、aop处理类上面的注解
@Around(value = "@within(myTestAnnotation)")
处理类上面的注解、执行类下面的每个方法时都会触发

2、aop处理方法上面的注解
@Around(value = "@annotation(myTestAnnotation)")
在执行特定方法时会触发

3、注意
value = "@annotation (参数名)" 是必须的,@within(参数名)、@annotation(参数名)中的参数名必须与所注释的方法内注解入参的参数名一致

比如:

 @Around(value = "@within(myTestAnnotation)")
 public Object processMethod(ProceedingJoinPoint point, MyTestAnnotation myTestAnnotation) throws Throwable {
     return process(point,myTestAnnotation);
 }
 @Around(value = "@annotation(myTestAnnotation)")
 public Object around(ProceedingJoinPoint point, MyTestAnnotation myTestAnnotation) throws Throwable {
     return process(point,myTestAnnotation);
 }

 

argNames = "" 配置不是必须的,但其配置的值一定要与 所注释方法内所有对象名一致! 否则无法匹配参数,启动报错!

比如: 

 @Around(value = "@within(myTestAnnotation)",argNames = "point,myTestAnnotation")
 public Object processMethod(ProceedingJoinPoint point, MyTestAnnotation myTestAnnotation) throws Throwable {
     return process(point,myTestAnnotation);
 }
 @Around(value = "@annotation(myTestAnnotation)",argNames = "point,myTestAnnotation")
 public Object around(ProceedingJoinPoint point, MyTestAnnotation myTestAnnotation) throws Throwable {
     return process(point,myTestAnnotation);
 }

 

3、代码

 

@Aspect
@Component
@Slf4j
public class MyTestAspect {

    // @within处理类上面的注解[执行类下面的每个方法都会触发]
    @Around(value = "@within(myTestAnnotation)")
    public Object processMethod(ProceedingJoinPoint point, MyTestAnnotation myTestAnnotation) throws Throwable {
        return process(point,myTestAnnotation);
    }


    // @annotation处理方法上面的注解
    @Around(value = "@annotation(myTestAnnotation)")
    public Object around(ProceedingJoinPoint point, MyTestAnnotation myTestAnnotation) throws Throwable {
        return process(point,myTestAnnotation);
    }

    public static Object process(ProceedingJoinPoint pjp, MyTestAnnotation myTestAnnotation) throws Throwable {
        // 获取当前方法参数[]
        Object[] args = pjp.getArgs();
        // 获取当前方法
        Signature method = pjp.getSignature();
        // 执行当前方法并返回结果
        Object returnValue = pjp.proceed(args);
        log.info("methodName = {} and args = {}  and returnValue ={} proceed finished !", method.getName(), args, returnValue);
        return returnValue;
    }
    
}

 

posted @ 2023-07-24 11:53  kelelipeng  阅读(3799)  评论(0编辑  收藏  举报