gpt4 book ai didi

java - 使用 AspectJ 捕获参数自定义注释

转载 作者:行者123 更新时间:2023-12-01 14:02:59 25 4
gpt4 key购买 nike

使用 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!";
}

输出

列表之前

长度==0

列表后

内部运行方法

如何获取自定义注释的参数,因为我根据参数做出一些决定! 即打印可能是真/假

更新我的信息!

最佳答案

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

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

关于java - 使用 AspectJ 捕获参数自定义注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19194988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com