我正在 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将其分隔为两种方法。

10-08 13:11