使用注解定义切点,怎么将多个切点注解组合为一个切点注解?

题目描述

使用注解定义切点,怎么将多个切点注解组合为一个切点注解?

相关代码

  • 切点注解

    @Target({ElementType.METHOD, ElementType.TYPE})

    @Retention(RetentionPolicy.RUNTIME)

    @Documented

    public @interface A {

    }

    =======================================================

    @Target({ElementType.METHOD, ElementType.TYPE})

    @Retention(RetentionPolicy.RUNTIME)

    @Documented

    public @interface B {

    }

  • Aspect

    @Aspect

    @Component

    public class AAspect {

    @Pointcut("@annotation(A)")

    private void cut() {

    }

    @Around("cut()")

    public Object around(ProceedingJoinPoint pjp) throws Throwable {

    System.out.println("A");

    return pjp.proceed();

    }

    }

    =======================================================

    @Aspect

    @Component

    public class BAspect {

    @Pointcut("@annotation(B)")

    private void cut() {

    }

    @Around("cut()")

    public Object around(ProceedingJoinPoint pjp) throws Throwable {

    System.out.println("B");

    return pjp.proceed();

    }

    }

  • 实现

    @Target({ElementType.METHOD, ElementType.TYPE})

    @Retention(RetentionPolicy.RUNTIME)

    @Documented

    @A

    @B

    public @interface All {

    }

  • 应用

    @RestController

    @RequestMapping("/test")

    public class TestController1 {

    @All

    @GetMapping("/all")

    public Object testAll(){

    return 1;

    }

    @A

    @GetMapping("/a")

    public Object testA(){

    return 1;

    }

    @B

    @GetMapping("/b")

    public Object testB(){

    return 1;

    }

    }

你期待的结果是什么?实际看到的错误信息又是什么?

  • 期待结果

请求/test/a 输出 A
请求/test/b 输出 B
请求/test/all 输出 A B

  • 实际结果
    请求/test/all 无任何输出


回答:

@Pointcut("@annotation(A)") 改为 @Pointcut("@annotation(A)||@annotation(All)")

@Pointcut("@annotation(B)") 改为 @Pointcut("@annotation(B)||@annotation(All)")


回答:

@Pointcut("execution(* runstatic.stools..controller.*.*(..))")

//两个..代表所有子目录,最后括号里的两个..代表所有参数

public void logPointCut() {

}

如果注解很多,请根据上面例子来,再通过一些反射工具,如spring的ReflectionUtils拿到注解

以上是 使用注解定义切点,怎么将多个切点注解组合为一个切点注解? 的全部内容, 来源链接: utcz.com/p/944432.html

回到顶部