相关疑难解决方法(0)

具有带注释参数的切入点匹配方法

在以下情况下,我需要使用与方法匹配的切入点创建方面:

  1. 它是用MyAnnotationForMethod注释的
  2. 其中一个参数(可以有很多)用@MyAnnotationForParam注释(但也可以有其他注释).

方面类看起来像这样

@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)

java parameters aop annotations aspectj

8
推荐指数
1
解决办法
9265
查看次数

如何根据带注释的参数编写Aspect切入点

我在解决如何创建一个可以在具有特定注释参数的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)

java spring annotations aspectj

7
推荐指数
1
解决办法
4132
查看次数

标签 统计

annotations ×2

aspectj ×2

java ×2

aop ×1

parameters ×1

spring ×1