springboot aop注解方式无法获取实现方法

项目中使用自定义注解@SolrHandle写在service的实现类上,使用以下代码无法获取此注解,获取到的SolrHandle为null。

1
2
3
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
SolrHandle solrHandle = method.getAnnotation(SolrHandle.class);

正确方式如下

1
2
3
4
5
6
7
8

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
//获取当前类的对象
Class<?> clazz = joinPoint.getTarget().getClass();
//获取当前类有 SolrHandle 注解的方法
method = clazz.getMethod(method.getName(), method.getParameterTypes());
SolrHandle solrHandle = method.getAnnotation(SolrHandle.class);