何时使用@Pointcut & @Before, @After AOP 注解

When to use @Pointcut & @Before, @After AOP Annotations

正在学习 Spring 中的 AOP 概念。我现在非常了解 @Before@After 注释的用法,并开始将其用于时间捕获目的。

这几乎可以满足我所有与 AOP 相关的需求。想知道每个 spring 指南谈论的 @pointcut 注释是什么?那是多余的功能吗?还是它有单独的需求?

简单来说,无论您在@Before 或@After 中指定什么,都是一个切入点表达式。这可以使用 @Pointcut 注释提取到一个单独的方法中,以便更好地理解、模块化和更好地控制。例如

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void requestMapping() {}

    @Pointcut("within(blah.blah.controller.*) || within(blah.blah.aspect.*)")
    public void myController() {}

    @Around("requestMapping() && myController()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
      ...............
   }  

如您所见,无需在 @Around 中指定切入点表达式,您可以使用 @Pointcut.

将其分成两个方法