Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring-访问Advice中注释的参数_Java_Spring_Annotations - Fatal编程技术网

Java Spring-访问Advice中注释的参数

Java Spring-访问Advice中注释的参数,java,spring,annotations,Java,Spring,Annotations,我有以下注解: public @interface Log { int id() default 0; } 现在,我想访问advice方法中的id字段。例如,如果我调用foo,那么应该打印它的id(1) @Log(id = 1) public void foo() {} 到目前为止,我得到了这个: @Pointcut("@annotation(com.skyfall.aspects.Log)") public void logPointcut(com.skyfall.aspe

我有以下注解:

public @interface Log {
    int id() default 0;
}
现在,我想访问advice方法中的id字段。例如,如果我调用foo,那么应该打印它的id(1)

@Log(id = 1)
public void foo() {}
到目前为止,我得到了这个:

@Pointcut("@annotation(com.skyfall.aspects.Log)")
    public void logPointcut(com.skyfall.aspects.Log log) {}

@Before("logPointcut(log))")
public void logBefore(JoinPoint joinPoint, com.skyfall.aspects.Log log) {
    System.out.println(log.id());
    }

但是,我发现一个错误,即“当使用@annotation(annotationtype(annotation field))时,必须绑定注释字段”

您的代码应该是这样的,因为您使用的是带有绑定形式格式的切入点,其中只使用参数名,注释类型取自
logBefore
方法:

@Pointcut("@annotation(log)")
public void logPointcut(com.skyfall.aspects.Log log) {}

@Before("logPointcut(log))")
public void logBefore(JoinPoint joinPoint, com.skyfall.aspects.Log log) {
    System.out.println(log.id());
}