mog*_*lol 13 aop spring spring-aop pointcut
说我有这样的方法:
public void method(@CustomAnnotation("value") String argument)
Run Code Online (Sandbox Code Playgroud)
是否有切入点表达式可以选择带有@CustomAnnotation注释的参数的所有方法?如果是这样,我可以获得访问的方式去"价值"参数?
Mik*_*tel 17
在选择你的论点时:
@Before("execution(* *(@CustomAnnotation (*)))")
public void advice() {
System.out.println("hello");
}
Run Code Online (Sandbox Code Playgroud)
参考:http://forum.springsource.org/archive/index.php/t-61308.html
在获取注释参数时:
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Annotation[][] methodAnnotations = method.getParameterAnnotations();
Run Code Online (Sandbox Code Playgroud)
将为您提供可以迭代的注释,并使用instanceof查找绑定的注释.我知道那是hacky但是afaik这是目前支持的唯一方式.
只是为了完成最后一个答案:
@Before(value="execution(* *(@CustomAnnotation (*), ..)) && args(input, ..)")
public void inspect(JoinPoint jp, Object input) {
LOGGER.info(">>> inspecting "+input+" on "+jp.getTarget()+", "+jp.getSignature());
}
Run Code Online (Sandbox Code Playgroud)
将匹配一个方法,其中(其中一个)方法的参数用注释 @CustomAnnotation,例如:
service.doJob(@CustomAnnotation Pojo arg0, String arg1);
Run Code Online (Sandbox Code Playgroud)
相比之下
@Before(value="execution(* *(@CustomAnnotation *, ..)) && args(input, ..)")
public void inspect(JoinPoint jp, Object input) {
LOGGER.info(">>> inspecting "+input+" on "+jp.getTarget()+", "+jp.getSignature());
}
Run Code Online (Sandbox Code Playgroud)
这将匹配其中(其中一个)参数的类用 注释的方法 @CustomAnnotation,例如:
service.doJob(AnnotatedPojo arg0, String arg1);
Run Code Online (Sandbox Code Playgroud)
其中 pojo 声明如下:
@CustomAnnotation
public class AnnotatedPojo {
}
Run Code Online (Sandbox Code Playgroud)
所有的区别都在于切入点声明中的@CustomAnnotation (*)vs. @CustomAnnotation *。
为了配合1..1标注的参数,无论其位置的使用
@Before("execution(* *(@CustomAnnotation (*), ..)) || " +
"execution(* *(.., @CustomAnnotation (*), ..)) || " +
"execution(* *(.., @CustomAnnotation (*)))")
public void advice(int arg1, @CustomAnnotation("val") Object arg2) { ... }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13345 次 |
| 最近记录: |