@Before 不允许切入点引用,为什么?

问题描述 投票:0回答:1

我可以做

    @Pointcut("@annotation(com.learning.validation.Validate)")
    public void validatePointCut() {
    }

    @AfterReturning(pointcut = "validatePointCut()")
    public void validate(JoinPoint joinPoint) {
    // some code
    }

但我做不到

    @Pointcut("@annotation(com.learning.validation.Validate)")
    public void validatePointCut() {
    }

    @Before(pointcut = "validatePointCut()")
    public void validate(JoinPoint joinPoint) {
    // some code
    }

我必须做

    @Before("@annotation(com.learning.validation.Validate)")
    public void validate(JoinPoint joinPoint) {
    // some code
    }

我希望使用@Before 与@AfterReturning 相同, 我正在使用 Spring Boot 3.x 和 Spring AOP 我是否遗漏了什么,或者它应该如何工作

java spring-boot aop aspectj spring-aop
1个回答
0
投票

原因是

@Before
只知道切入点表达式的默认参数名称
value
,而
@AfterReturning
为其定义了一个额外的别名
pointcut

即,对于

@Before
,您可以写任何

  • @Before("validatePointCut()")
    ,
  • @Before(value = "validatePointCut()")
    ,

而对于

@AfterReturning
你可以写任何

  • @AfterReturning("validatePointCut()")
    ,
  • @AfterReturning(value = "validatePointCut()")
    ,
  • @AfterReturning(pointcut = "validatePointCut()")
    .

我建议您学习如何使用 IDE 的功能,以便在输入程序时能够直接从代码编辑器中找到您使用的 API。

© www.soinside.com 2019 - 2024. All rights reserved.