網站首頁 編程語言 正文
在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-09-30 python實現圖像降噪_python
- 2022-03-06 Linux之Centos8創建CA證書教程_Linux
- 2022-05-10 springMVC 文件上傳和下載
- 2022-09-06 Go結構體SliceHeader及StringHeader作用詳解_Golang
- 2022-07-19 react組件通訊的三種方式props:父組件和子組件互相通訊、兄弟組件通訊
- 2022-06-21 git基礎之各版本控制系統介紹_其它綜合
- 2023-04-02 Python中的常見數據集打亂方法_python
- 2022-05-06 C#迭代器方法介紹_C#教程
- 最近更新
-
- 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同步修改后的遠程分支