当前位置:网站首页>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
参考
边栏推荐
猜你喜欢
随机推荐
后台登录模块以及验证码登录
【HMS core】【ML kit】Machine Learning Service FAQ
Timing Constraint Basics
PWM输出模块PCA9685
74HC595 的使用
如何选型APS系统,还需明确这七大关键因素
硅光电池采集用于植物叶片农残检测
【Harmony OS】【ARK UI】Lightweight Data Storage
Parameters in dynamic libraries cannot be modified through macro definitions or global variables in header files
地理空间分析库turf.js的学习
Still don't know what business intelligence (BI) is?After reading this article, you will understand
软件测试的方法详细介绍
什么是通用微处理器、单片机、DSP芯片、嵌入式系统?
明明加了唯一索引,为什么还是产生重复数据?
Anonymous shared memory ashmem
【计算机网络-哈工大】---学习笔记(下)---(二)Web安全威胁、SSL\IPsec、虚拟专用网、防火墙
Faced with risk control, what should Amazon do when evaluating self-supporting accounts?
php uses phpoffice/phpspreadsheet to import and export excel tables
面向6G的欠采样相移键控可见光调制方案
【luogu U142356】勇者的后缀(SA)(主席树)(二分)









