当前位置:网站首页>cglib获取不到接口注解
cglib获取不到接口注解
2022-08-09 05:12:00 【愤怒的苹果ext】
背景
- 我想实现的是获取
interface
类上的注解,代理使用的是cglib
。 - 这是我的注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface HbServerName {
/**
* 服务名称
* **/
String value() default "";
/***
* 地址前缀
* */
String serverUrlPrefix() default "";
}
- 我获取的是一个接口上的
HbServerName
注解。但是返回是null
。
解决方案
- 我对比了以下几种方式
...省略...
// target是我构造器传进来的
HbServerName annotation = target.getAnnotation(HbServerName.class);
HbServerName annotation2 = obje.getClass().getAnnotation(HbServerName.class);
HbServerName annotation1 = Class.forName(target.getName()).getAnnotation(HbServerName.class);
HbServerName hbServerName = AnnotationUtils.findAnnotation(obje.getClass(), HbServerName.class);
...省略...
- 运行结果如下图所示。
- 可以看到这2种办法是可以的。
HbServerName annotation1 = Class.forName(target.getName()).getAnnotation(HbServerName.class);
HbServerName hbServerName = AnnotationUtils.findAnnotation(obje.getClass(), HbServerName.class);
Class.forName重新得到class
或者使用Spring的AnnotationUtils#findAnnotation
参考
边栏推荐
- 如何选型APS系统,还需明确这七大关键因素
- C Advanced-C Language File Operation
- C语言之内存函数
- C进阶 - 程序的编译(预处理操作) + 链接
- 软件测试的发展趋势
- 【LeetCode】136. 只出现一次的数字
- 步进电机驱动的使用及程序
- [Harmony OS] [ArkUI] ets development graphics and animation drawing
- 初识二叉树
- 【HMS core】【Ads Kit】Huawei Advertising——Overseas applications are tested in China. Official advertisements cannot be displayed
猜你喜欢
随机推荐
如何让Win11两个屏幕任务栏都显示时间?
Faced with risk control, what should Amazon do when evaluating self-supporting accounts?
p67 mixin 混入/混合:两个组件共享一个配置
could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarExcept
Perl basic grammar
【luogu U142356】Suffix of the Brave (SA) (Chairman Tree) (2 points)
【Harmony OS】【ARK UI】Date 基本操作
2022-08-08 顾宇佳 学习笔记
如何剪裁svg并压缩
Address book (dynamic version) (C language) (VS)
【Harmony OS】【ARK UI】Custom popup
【Harmony OS】【ArkUI】ets开发 图形与动画绘制
软件测试的发展趋势
电气规则
匿名共享内存 ashmem
时序约束基础
TASSEL software imports plink format file error
剑指Offer-二叉树路径问题总结
地理空间分析库turf.js的学习
中断系统结构及中断控制详解