網站首頁 編程語言 正文
在spring boot中,自定義方法注解,在有其他注解存在的情況下,可能出現無法獲取自定義注解的情況
利用ApplicationContext掃描時,通過debug得到class文件名含有EnhancerBySpringCGLIB
,類似于這樣:
或含有$ProxyXXX
,類似于這樣:
1 原因
其根本原因在于,applicationContext.getBeansWithAnnotation(類注解.class)
方法獲取到的Bean,是GClib代理后的類或者Jdk代理的類,導致 bean.getClass().getDeclaredMethods()
拿不到原真實類的方法
需要根據情況,利用反射去獲取對應真實類,拿到其方法
2 解決方案
這里封裝了兩個注解,用于監聽MQ消息:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MqttClient {
String value() default "";
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MqttListener {
/**
* TOPIC,支持處理多個消息
*
* @return
*/
String[] value() default {};
/**
* QOS
*
* @return
*/
int qos() default 0;
}
其中,@MqttClient
類注解,@MqttListener
方法注解
新增一個配置類實現CommandLineRunner
,實現其run方法,掃描對應注解的bean
@Component
@Slf4j
public class MqttAnnotationScanner implements CommandLineRunner {
@Autowired
private ApplicationContext applicationContext;
@Override
public void run(String... args) throws Exception {
Map<String, Object> beansMap = applicationContext.getBeansWithAnnotation(MqttClient.class);
for (Object bean : beansMap.values()) {
Method[] declaredMethods = null;
//判斷是否為JdkDynamicProxy代理類
if (AopUtils.isJdkDynamicProxy(bean)){
Object singletonTarget = AopProxyUtils.getSingletonTarget(bean);
if (singletonTarget != null) {
declaredMethods = singletonTarget.getClass().getDeclaredMethods();
}
} else if (AopUtils.isCglibProxy(bean)) {//判斷是否為CglibProxy代理類
declaredMethods = bean.getClass().getSuperclass().getDeclaredMethods();
} else {
declaredMethods = bean.getClass().getDeclaredMethods();
}
if (declaredMethods != null) {
for (Method method : declaredMethods) {
MqttListener annotation = method.getAnnotation(MqttListener.class);
if (annotation != null) {
// ......執行相應的邏輯
}
}
}
}
}
}
這樣,就可以拿到對應的CGlib代理類的注解方法了,實現與其他注解共存
@RedisLock(lockKey = "{{#entity.uid}}")
@MqttListener(Topic.TEST)
public void test(MqttEntity entity) {
log.info("接收到的數據:{}", entity);
testSync(entity.getValues());
}
原文鏈接:https://blog.csdn.net/Vampire_1122/article/details/125415105
相關推薦
- 2022-06-15 Go?interface?接口的最佳實踐經驗分享_Golang
- 2022-05-11 springboot多版本管理
- 2023-01-01 c語言中實現數組幾個數求次大值_C 語言
- 2022-07-12 Hive獲取當天0點時間,條件查詢某一天數據
- 2022-05-28 Redis如何使用樂觀鎖(CAS)保證數據一致性_Redis
- 2022-06-18 Android?Recyclerview實現左滑刪除功能_Android
- 2022-06-02 Go語言中定時任務庫Cron使用方法介紹_Golang
- 2022-10-04 Python的getattr函數方法學習使用示例_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支