带注释参数的 Spring AOP 切入点 [英] Spring AOP pointcut for annotated argument

查看:28
本文介绍了带注释参数的 Spring AOP 切入点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的方法:

Say I have a method like so:

public void method(@CustomAnnotation("value") String argument)

是否有一个切入点表达式可以选择所有带有@CustomAnnotation 注释的参数的方法?如果是这样,有没有办法访问值"参数?

Is there a pointcut expression that could select all methods with arguments annotated with @CustomAnnotation? If so is there a way I could get access go the "value" argument?

推荐答案

关于选择参数:

@Before("execution(* *(@CustomAnnotation (*)))")
public void advice() {
System.out.println("hello");
}

参考:http://forum.springsource.org/archive/index.php/t-61308.html

获取注解参数:

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Annotation[][] methodAnnotations = method.getParameterAnnotations();

将为您提供可以迭代的注释并使用 instanceof 来查找绑定的注释.我知道那是 hacky 但 afaik 这是目前支持的唯一方法.

Will get you the annotations which you can iterate and use instanceof to find your bound annotation. I know thats hacky but afaik this is the only way supported currently.

这篇关于带注释参数的 Spring AOP 切入点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆