Spring代理對象導(dǎo)致的獲取不到原生對象注解的解決
問題描述
我在接受 mq 消息的時候,需要做一個重試次數(shù)限制,如果超過 maxNum 就發(fā)郵件告警,不再重試。
所以我需要對 consumer 對象進行代理,然后如果超過異常次數(shù),我直接返回成功,并且發(fā)送成功消息,但是我獲取 consumer handler 方法的方式是通過 method.getAnnotation(XXClient.class) 方式,那么就會返回 null。
問題示例代碼
目標(biāo)類, 我這里就之定義一個 test 方法,里面做一些個簡單的打印。
@Component public class TestBean { ? ? @Anno ? ? public void test() { ? ? ? ? System.out.println("test ....."); ? ? } }
代理邏輯邏輯處理, 主要就是做一個 @Around 的方法覆蓋,保證在調(diào)用目標(biāo)方法之前,先輸出我插入的邏輯。
@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(); ? ? } }
調(diào)用點, 通過 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) 無法獲取到注解信息,所以執(zhí)行 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); }
這里他會做一個判斷,如果是代理對象就調(diào)用 ReflectionUtils.getAllDeclaredMethods 獲取所有的方法, 然后再去拿注解的時候二次判斷一下,如果存在代理,那么就通過 AnnotationUtils.findAnnotation 感覺是相當(dāng)?shù)膰?yán)謹(jǐn)。
總結(jié)
Spring 提供了非常強大的一站式開發(fā)功能,而且還提供了比較優(yōu)秀的工具方法比如: BeanUtils 、ReflectionUtils 、AnnotationUtils 等,這些都是我們值得掌握的基礎(chǔ)工具類。
參考資料
https://www.jianshu.com/p/b69e64121b97
到此這篇關(guān)于Spring代理對象導(dǎo)致的獲取不到原生對象注解的解決的文章就介紹到這了,更多相關(guān)Spring獲取不到原生對象注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用編程方式配置DataSource的方法
這篇文章主要介紹了SpringBoot使用編程方式配置DataSource的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01feignclient?https?接口調(diào)用報證書錯誤的解決方案
這篇文章主要介紹了feignclient?https?接口調(diào)用報證書錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03SpringSecurity整合springBoot、redis實現(xiàn)登錄互踢功能
這篇文章主要介紹了SpringSecurity整合springBoot、redis實現(xiàn)登錄互踢,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05Java中ShardingSphere分庫分表實戰(zhàn)
我們做項目的時候,數(shù)據(jù)量比較大,單表千萬級別的,需要分庫分表,本文主要介紹了Java中ShardingSphere分庫分表實戰(zhàn),感興趣的可以了解一下2021-09-09Intellij IDEA 2017新特性之Spring Boot相關(guān)特征介紹
Intellij IDEA 2017.2.2版本針對Springboot設(shè)置了一些特性,本篇文章給大家簡單介紹一下如何使用這些特性,需要的朋友參考下吧2018-01-01