【发布时间】:2016-06-03 08:25:08
【问题描述】:
我正在使用 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()查看时,它不起作用。
【问题讨论】:
标签: aspectj spring-aop pointcut aspects spring-aspects