Java Spring AOP-提取方法和参数注释
我有一个方法注释(@MethodAnno)和一个参数注释(@p)。我需要创建一个方面来捕获用@MethodAnno注释的方法的调用,并查找用@P注释的方法参数。然而,虽然我能够在我的方面中获得方法注释,但参数注释不会在MethodSignature中返回。下面是我所拥有的 注释Java Spring AOP-提取方法和参数注释,java,spring,aop,spring-aop,aspect,Java,Spring,Aop,Spring Aop,Aspect,我有一个方法注释(@MethodAnno)和一个参数注释(@p)。我需要创建一个方面来捕获用@MethodAnno注释的方法的调用,并查找用@P注释的方法参数。然而,虽然我能够在我的方面中获得方法注释,但参数注释不会在MethodSignature中返回。下面是我所拥有的 注释 @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface MethodAn
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface MethodAnno {
Foo[] foo();
}
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface P {
Bar value();
}
方面
@Aspect
@Component
public class MyAspect {
@Before("@annotation(methodAnnotation)")
public void methodsWithMethodLevelAnnotation(final JoinPoint pjp, MethodAnno methodAnnotation) {
MethodSignature signature = (MethodSignature) pjp.getSignature();
Annotation[][] parameterAnnotations = signature.getMethod().getParameterAnnotations();
// parameterAnnotations is always a single element array and the element is a zero-length array
}
}
用法
public interface Service {
public void execute(String input);
}
@Service
public class ServiceImpl implements Service {
@Override
@MethodAnno
public void execute(@P String input) {
....
}
}
我看到的一个错误是,切入点表达式检查
“@annotation(methodAnnotation)”
,而您定义的实际注释名为MethodAnno
。在我的切入点中使用@annotation(methodAnnotation),其中“methodAnnotation”是切入点方法参数名称的名称,允许我获取我的方法注释MethodAnno.Oops的一个实例,你是对的-我为误读道歉。因为您的类正在实现一个接口,所以您需要获取正在调用的实际方法。看答案。谢谢!!如果你加上你的评论作为回答,我会接受。没关系,很高兴我能帮上忙。你应该把我链接到的另一个答案投上去