Aspectj @Before可以工作,但@Pointcut没有

时间:2018-04-24 09:05:27

标签: spring-boot aspectj

我在spring boot应用程序中有一个简单的aspectj实现示例,我试图让每个不同的方法都有效。

如果我使用以下

  @Before("execution(* com.example.test.controllers.ProductController.deleteProduct(..))")
  public void specificInterception() {
    System.out.println("pointcut interception working");
  }

这很好用,我可以看到println

的控制台输出

但是,如果我使用

  @Pointcut("execution(* com.example.test.controllers.ProductController.deleteProduct(..))")
  public void specificInterception() {
    System.out.println("pointcut interception working");
  }

无法触发。为什么这不起作用?

1 个答案:

答案 0 :(得分:2)

使用@Pointcut注释的方法本身并不做任何事情。它只允许您多次重复使用相同的表达式,而不必重新声明它。我甚至不确定是否会调用这种方法的方法体。

在您的情况下,您可以像这样使用切入点:

@Pointcut("execution(* com.example.test.controllers.ProductController.deleteProduct(..))")
public void specificPointcut() {}


@Before("specificPointcut()")
public void specificInterception() {
    System.out.println("pointcut interception working");
}