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());
}