Spring代理对象导致的获取不到原生对象注解的解决
作者:心城以北 时间:2021-12-05 11:44:19
问题描述
我在接受 mq 消息的时候,需要做一个重试次数限制,如果超过 maxNum 就发邮件告警,不再重试。
所以我需要对 consumer 对象进行代理,然后如果超过异常次数,我直接返回成功,并且发送成功消息,但是我获取 consumer handler 方法的方式是通过 method.getAnnotation(XXClient.class) 方式,那么就会返回 null。
问题示例代码
目标类, 我这里就之定义一个 test 方法,里面做一些个简单的打印。
@Component
public class TestBean {
@Anno
public void test() {
System.out.println("test .....");
}
}
代理逻辑逻辑处理, 主要就是做一个 @Around 的方法覆盖,保证在调用目标方法之前,先输出我插入的逻辑。
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Anno {
String key() default "100%";
}
@Aspect
@Component
public class AnnoAspect {
@Around("@annotation(anno)")
public Object anno(ProceedingJoinPoint point, Anno anno) throws Throwable {
System.out.println("anno invoke!!!!!!");
return point.proceed();
}
}
调用点, 通过 AnnotationConfigApplicationContext 获取 bean. 然后通过 getMethods() 获取所有的方法,最后查找 Anno 注解的 Method 对象。
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanStart.class);
TestBean bean = applicationContext.getBean(TestBean.class);
Class<? extends TestBean> classz = bean.getClass();
Method[] methods = classz.getMethods();
for (Method m : methods) {
Anno annotation = m.getAnnotation(Anno.class);
if (annotation != null) {
System.out.println(" ============= invoke test ===========");
m.invoke(bean, new Object());
}
}
由于 m.getAnnotaion(Anno.class) 无法获取到注解信息,所以执行 test 方法失败,
到此问题还原完毕,我们再来看看如何解决。
解决方案
通过 Anno ao = AnnotationUtils.findAnnotation(classz, Anno.class); 方法获取即可。
有的代码是这样写的 :
String name = classz.getName();
boolean isSpringProxy = name.indexOf("SpringCGLIB$$") >= 0;
Method[] methods;
if (isSpringProxy) {
methods = ReflectionUtils.getAllDeclaredMethods(AopUtils.getTargetClass(bean));
} else {
methods = classz.getMethods();
}
// 省略部分代码
if (isSpringProxy) {
annotation = AnnotationUtils.findAnnotation(method, MqClient.class);
} else {
annotation = method.getAnnotation(Anno.class);
}
这里他会做一个判断,如果是代理对象就调用 ReflectionUtils.getAllDeclaredMethods 获取所有的方法, 然后再去拿注解的时候二次判断一下,如果存在代理,那么就通过 AnnotationUtils.findAnnotation 感觉是相当的严谨。
来源:https://juejin.cn/post/7081635762562662413
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JDBC连接MySQL并实现模糊查询
Java基于余弦方法实现的计算相似度算法示例
SpringBoot 整合jdbc和mybatis的方法
![](https://img.aspxhome.com/file/2023/8/58158_0s.png)
Jenkins的安装配置详解
![](https://img.aspxhome.com/file/2023/6/62166_0s.png)
C#操作串口通信协议Modbus的常用方法介绍
Java实现LeetCode(54.螺旋矩阵)
Unity shader实现消融效果
![](https://img.aspxhome.com/file/2023/4/66584_0s.gif)
Java Thread 类和Runnable 接口详解
![](https://img.aspxhome.com/file/2023/7/59017_0s.png)
Java泛型通配符的使用详解
详解Java线程同步器CountDownLatch
![](https://img.aspxhome.com/file/2023/2/58342_0s.png)
Spring声明式事务和@Aspect的拦截顺序问题的解决
Flutter 日历组件简单实现
SpringBoot定时任务设计之时间轮案例原理详解
![](https://img.aspxhome.com/file/2023/0/62450_0s.png)
Android实现人脸支付的示例代码
![](https://img.aspxhome.com/file/2023/0/86020_0s.gif)
Java利用Dijkstra算法求解拓扑关系最短路径
![](https://img.aspxhome.com/file/2023/0/68510_0s.jpg)
在Maven下代理服务器设定的方式
![](https://img.aspxhome.com/file/2023/9/58569_0s.png)
Spring Boot + Mybatis-Plus实现多数据源的方法
![](https://img.aspxhome.com/file/2023/5/59225_0s.png)
java中map和对象互转工具类的实现示例
![](https://img.aspxhome.com/file/2023/2/62162_0s.png)
mybatis-plus的批量新增/批量更新以及问题
![](https://img.aspxhome.com/file/2023/2/67972_0s.png)
El表达式使用问题javax.el.ELException:Failed to parse the expression的解决方式
![](https://img.aspxhome.com/file/2023/8/59528_0s.png)