我正在使用 Spring AOP 来拦截方法执行。
我有一个如下所示的界面:
public interface MyAwesomeService {
public Response doThings(int id, @AwesomeAnnotation SomeClass instance);
}
下面是接口的实现:
public class MyAwesomeServiceImpl implements MyAwesomeService {
public Response doThings(int id, SomeClass instance) {
// do something.
}
}
现在我希望任何具有@AwesomeAnnotation 注释的参数的方法都应该被Spring AOP 捕获。
所以我写了以下有效的方面。
@Aspect
@Component
public class MyAwesomeAspect {
@Around("myPointcut()")
public Object doAwesomeStuff(final ProceedingJoinPoint proceedingJoinPoint) {
final MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
Annotation[][] annotationMatrix = methodSignature.getMethod().getParameterAnnotations();
// annotationMatrix is empty.
}
@Pointcut("execution(public * *(.., @package.AwesomeAnnotation (package.SomeClass), ..))")
public void myPointcut() {}
}
但是,当我尝试查找参数注释时,我没有得到任何注释。如上所述,annotationMatrix 是空的。
所以这是我的问题:
- 为什么 annotationMatrix 是空的?可能是因为参数注释不是从接口继承的。
- 为什么我能够捕获方法执行。由于 Spring AOP 能够匹配切入点,因此 Spring 以某种方式能够看到方法的参数注释,但是当我尝试看到使用
methodSignature.getMethod().getParameterAnnotations()
它时不起作用。