Spring AOP:获取切入点注释的参数

发布于 2024-10-21 18:05:52 字数 432 浏览 3 评论 0原文

考虑我已经定义了以下方面:

@Aspect
public class SampleAspect {

    @Around(value="@annotation(sample.SampleAnnotation)")
    public Object display(ProceedingJoinPoint joinPoint) throws Throwable {
        // ...
    }
}

和注释

public @interface SampleAnnotation {
    String value() default "defaultValue";
}

如果我的方面,有没有办法在显示方法中读取注释SampleAnnotation的值参数?

感谢您的帮助, 埃里克

Consider I have defined the following aspect:

@Aspect
public class SampleAspect {

    @Around(value="@annotation(sample.SampleAnnotation)")
    public Object display(ProceedingJoinPoint joinPoint) throws Throwable {
        // ...
    }
}

and the annotation

public @interface SampleAnnotation {
    String value() default "defaultValue";
}

Is there a way to read the value parameter of the annotation SampleAnnotation in the display method if my aspect?

Thanks for your help,
erik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愚人国度 2024-10-28 18:05:52

将建议签名更改为

@Around(value="@annotation(sampleAnnotation)")
public Object display(ProceedingJoinPoint joinPoint, SampleAnnotation sampleAnnotation ) throws Throwable {
    // ...
}

,您将可以访问注释中的值。

请参阅 文档了解更多信息。

Change the advice signature to

@Around(value="@annotation(sampleAnnotation)")
public Object display(ProceedingJoinPoint joinPoint, SampleAnnotation sampleAnnotation ) throws Throwable {
    // ...
}

and you will have access to the value in the annotation.

See docs for more info.

夏雨凉 2024-10-28 18:05:52

下面我将添加一个完整的 AOP 实现示例,其中我将从自定义 pointCut 注释中获取参数,其中我的建议旨在计算函数的执行时间:

1- 自定义注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationLogExecutionTime {

    public boolean isActivate() default false;

}

2- 控制器:

@AnnotationLogExecutionTime(isActivate = true)
@PostMapping("/connection")
public HttpEntity<String> createAuthenticationToken(HttpServletRequest request,
                                                        @RequestBody AuthenticationRequest authenticationRequest) {...}

3- 建议

@Component
@Aspect
public class LoggingExecutionTimeAdvice {

    @Around("@annotation(annotationLogExecutionTime)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint, AnnotationLogExecutionTime annotationLogExecutionTime) throws Throwable {

        if(annotationLogExecutionTime.isActivate()){//Here I recover the value!!!!
            long start = System.currentTimeMillis();
            Object proceed = joinPoint.proceed();
            long executionTime = System.currentTimeMillis() - start;
            System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
            return proceed;
        }
        Object proceed = joinPoint.proceed();
        return proceed;
    }
}

解释:

我们的建议(logExecutionTime)将在(joinPoint)周围执行,该函数将用AnnotationLogExecutionTime(我们的自定义注释)进行注释,所以我想激活或不激活这个计算时间执行,所以我将从我们的自定义注释的成员中获取值(您询问;))

Bellow I'll add a complete example of AOP implementation where I'll getting parameter from my Custom pointCut annotation, where my advice aim to calculate the time execution of a function:

1- Custom Annotation:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationLogExecutionTime {

    public boolean isActivate() default false;

}

2- Controller:

@AnnotationLogExecutionTime(isActivate = true)
@PostMapping("/connection")
public HttpEntity<String> createAuthenticationToken(HttpServletRequest request,
                                                        @RequestBody AuthenticationRequest authenticationRequest) {...}

3- Advice

@Component
@Aspect
public class LoggingExecutionTimeAdvice {

    @Around("@annotation(annotationLogExecutionTime)")
    public Object logExecutionTime(ProceedingJoinPoint joinPoint, AnnotationLogExecutionTime annotationLogExecutionTime) throws Throwable {

        if(annotationLogExecutionTime.isActivate()){//Here I recover the value!!!!
            long start = System.currentTimeMillis();
            Object proceed = joinPoint.proceed();
            long executionTime = System.currentTimeMillis() - start;
            System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
            return proceed;
        }
        Object proceed = joinPoint.proceed();
        return proceed;
    }
}

Explanation:

Our advice (logExecutionTime) will be excuted around (joinPoint) the function that will be annotated with AnnotationLogExecutionTime (our custom annotation) so I want to active or not this the calculation of time execution so I'll get the value from the membre of our custom annotation (which you ask about ;) )

山色无中 2024-10-28 18:05:52

我之前对如何绘制此图感到困惑。

@Before("@annotation(any_variable_name) && args(spring_will_find_its_type_to_match)")
public void myAdvice(JoinPoint joinPoint, MyAnnotation any_variable_name, MyClass spring_will_find_its_type_to_match) {
    System.out.println("before testing in Aspect myAdvice " + spring_will_find_its_type_to_match);
}

I was confused on how to map this before.

@Before("@annotation(any_variable_name) && args(spring_will_find_its_type_to_match)")
public void myAdvice(JoinPoint joinPoint, MyAnnotation any_variable_name, MyClass spring_will_find_its_type_to_match) {
    System.out.println("before testing in Aspect myAdvice " + spring_will_find_its_type_to_match);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文