gpt4 book ai didi

spring - 何时使用@Pointcut & @Before、@After AOP 注解

转载 作者:行者123 更新时间:2023-12-04 23:15:13 26 4
gpt4 key购买 nike

我在 Spring 中学习 AOP 概念。我现在很清楚 @Before 的用法和 @After注释并开始将其用于时间捕获目的。

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

最佳答案

简单来说,您在 @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 将其分为两种方法.

关于spring - 何时使用@Pointcut & @Before、@After AOP 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43844096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com