带注释参数的 Spring AOP 切入点

带注释参数的 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.