在以下情况下,我需要使用与方法匹配的切入点创建方面:
方面类看起来像这样
@Pointcut("execution(@MyAnnotationForMethod * *(..,@aspects.MyAnnotationForParam Object, ..)) && args(obj)")
void myPointcut(JoinPoint thisJoinPoint, Object obj) {
}
@Before("myPointcut(thisJoinPoint , obj)")
public void doStuffOnParam(JoinPoint thisJoinPoint, Object obj) {
LOGGER.info("doStuffOnParam :"+obj);
}
Run Code Online (Sandbox Code Playgroud)
注释方法
@MyAnnotationForMethod
public string theMethod(String a, @MyAnnotationForParam @OtherAnnotation Object obj, Object b){
LOGGER.info(a+obj+b);
}
Run Code Online (Sandbox Code Playgroud)
用eclipse - >警告:关于poincut:
Multiple markers at this line
- no match for this type name: MyAnnotationForMethod [Xlint:invalidAbsoluteTypeName]
- no match for this type name: aspects.MyAnnotationForParam On the before : advice defined in xxx.xxx.xxx.xxx.MyAspect has not …Run Code Online (Sandbox Code Playgroud) 我在解决如何创建一个可以在具有特定注释参数的bean上运行的切入点时遇到了一些麻烦.我最终的目标是在处理参数之前验证参数的值,但目前我只需要创建切入点.
请考虑以下注释
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PARAMETER })
public @interface MyAnnotation {}
Run Code Online (Sandbox Code Playgroud)
然后,我想将其应用于以下方法:
public void method1(@MyAnnotation long i) {}
public void method2(String someThing, @MyAnnotation long i) {}
public void method3(String someThing, @MyAnnotation long i, byte value) {}
Run Code Online (Sandbox Code Playgroud)
所以
我的切入点实现需要有以下几点:
@Before(value = "* *(..) && args(verifyMe)")
public void verifyInvestigationId(long verifyMe) {}
Run Code Online (Sandbox Code Playgroud)
我对这个@Before值究竟是什么以及如何将注释及其类型联系起来感到有些困惑.在这一点上,可能不值得列出我尝试过的东西!
更新:基于我在http://stackoverflow.com/questions/3565718/pointcut-matching-methods-with-annotated-parameters/3567170#3567170中看到的建议(纠正了一些误解并增加了我忽略的空间) )我已经到了以下工作的地步:
@Before("execution(public * *(.., @full.path.to.MyAnnotation (*), ..))")
public void beforeMethod(JoinPoint joinPoint) {
System.out.println("At least one of the parameters are annotated with @MyAnnotation"); …Run Code Online (Sandbox Code Playgroud)