【问题标题】:Custom Annotation with AspectJ capture parameters使用 AspectJ 捕获参数的自定义注释
【发布时间】:2013-10-07 23:14:02
【问题描述】:

使用 AspectJ 自定义注释

自定义注释

@Documented
@Target(ElementType.METHOD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {

String getString() default "";
boolean print() default true;

}

方面

@Before("execution(@Loggable  * *.*(..))")
public void myBeforeAdvice(JoinPoint jp) {

    System.out.println("Before List");

    Object[] parameterList = jp.getArgs();

    System.out.println("Length=="+ parameterList.length);

    System.out.println("After List");

    //return returnVal;
}

自定义注释使用

@Loggable(getString="Custom", print=true)
public String run(){

    System.out.println("Inside Run Method");

    return "Returning Method Run!";
}

OutPut

Before List

Length==0

After List

Inside Run Method

如何获取自定义注释的参数,因为我根据参数做出一些决定! i.e print may be true / false

更新我!

【问题讨论】:

    标签: java parameters annotations aop aspectj


    【解决方案1】:

    before(Loggable l) : call(@Loggable * *.*(..)) && @annotation(l);

    认为它与基于注释的切入点类似。

    【讨论】:

      猜你喜欢
      • 2015-08-15
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      相关资源
      最近更新 更多