Java Reflect如何利用反射獲取屬性上的注解
1. AnnotatedElement接口
AnnotatedElement接口表示目前正在此 JVM 中運行的程序的一個已注釋元素,該接口允許反射性地讀取注釋。
調(diào)用AnnotatedElement對象的如下方法可以訪問Annotation信息:
getAnnotation(Class<T>annotationClass):返回該程序元素上存在的指定類型的注釋,如果該類型的注釋不存在,則返回null。Annotation[] getAnnotations():返回此元素上存在的所有注釋。boolean isAnnotationPresent(Class<?extendsAnnotation>annotationClass):判斷該程序元素上是否存在指定類型的注釋,如果存在則返回true,否則返回false。Annotation[] getDeclaredAnnotations():返回直接存在于此元素上的所有注釋。與此接口中的其他方法不同,該方法將忽略繼承的注釋。
2. Field類實現(xiàn)了AnnotatedElement接口

3. 獲取屬性上的注解
① 自定義注解
@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD,ElementType.TYPE,ElementType.FIELD,ElementType.PARAMETER} )
public @interface MyParam1 {
String value() default "";
}@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = {ElementType.METHOD,ElementType.TYPE,ElementType.FIELD,ElementType.PARAMETER} )
public @interface MyParam2 {
String value() default "";
}② 給方法參數(shù)上添加注解
@ControllerAdvice
@Controller
@MyAnnotation(name = "李四",age=12)
public class Test {
@MyField1("name1")
@MyField2("name2")
@Value("name")
private String name;
@MyField1("email1")
@MyField2("email2")
@Value("email")
private String email;
}③ 獲取屬性上的注解
public class Main {
public static void main(String[] args) throws NoSuchMethodException, ClassNotFoundException {
// 得到Class類對象
Class<?> clazz = Class.forName("com.example.redislock.annotation.Test");
// 獲取類的所有屬性
Field[] fields = clazz.getDeclaredFields();
// 獲取屬性上的所有注解
int i = 1;
for (Field field : fields) {
System.out.println("第"+i+++"個屬性的注解有:");
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation.annotationType());
}
}
// 獲取屬性上指定MyField2類型的注解
System.out.println();
System.out.println("獲取屬性上指定MyField2類型的注解:");
for (Field field : fields) {
MyField2 myField2 = field.getAnnotation(MyField2.class);
System.out.println(myField2);
System.out.println(myField2.value());
}
// 獲取屬性上指定MyField2類型的注解
System.out.println();
System.out.println("獲取屬性上指定MyField2類型的注解:");
for (Field field : fields) {
MyField2[] myField2s = field.getAnnotationsByType(MyField2.class);
for (MyField2 myField2 : myField2s) {
System.out.println(myField2);
}
}
}
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用Springboot自定義轉(zhuǎn)換器實現(xiàn)參數(shù)去空格功能
這篇文章主要介紹了使用Springboot自定義轉(zhuǎn)換器實現(xiàn)參數(shù)去空格功能,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08
使用SpringBoot實現(xiàn)Redis多數(shù)據(jù)庫緩存
在我的系統(tǒng)中,為了優(yōu)化用戶行為數(shù)據(jù)的存儲與訪問效率,我引入了Redis緩存,并將數(shù)據(jù)分布在不同的Redis數(shù)據(jù)庫中,通過這種方式,可以減少單一數(shù)據(jù)庫的負載,提高系統(tǒng)的整體性能,所以本文給大家介紹了使用SpringBoot實現(xiàn)Redis多數(shù)據(jù)庫緩存,需要的朋友可以參考下2024-06-06
SpringCloud Gateway動態(tài)轉(zhuǎn)發(fā)后端服務實現(xiàn)過程講解
這篇文章主要介紹了SpringCloud Gateway動態(tài)轉(zhuǎn)發(fā)后端服務實現(xiàn)過程,簡單的路由轉(zhuǎn)發(fā)可以通過SpringCloudGateway的配置文件實現(xiàn),在一些業(yè)務場景種,會需要動態(tài)替換路由配置中的后端服務地址,單純靠配置文件無法滿足這種需求2023-03-03
SpringBoot中使用異步線程導致Request請求頭丟失問題的解決方法
異步線程請求頭丟失的問題通常發(fā)生在多線程環(huán)境中,特別是在使用 CompletableFuture 或其他異步編程模型時,本文給大家詳細介紹了SpringBoot中使用異步線程導致Request請求頭丟失問題的原因和解決方法,需要的朋友可以參考下2025-07-07
SpringBoot整合Graylog做日志收集實現(xiàn)過程
這篇文章主要為大家介紹了SpringBoot整合Graylog做日志收集實現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
MyBatis實現(xiàn)動態(tài)SQL模糊查詢的示例代碼
在數(shù)據(jù)庫查詢中,模糊查詢是最常用的功能之一,今天我們來探討如何通過動態(tài)SQL實現(xiàn)靈活、安全的模糊查詢,文中的示例代碼講解詳細,有需要的小伙伴可以了解下2026-02-02
springboot快速集成mybatis-plus的詳細教程
這篇文章主要介紹了springboot快速集成mybatis-plus的教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
SpringBoot集成Swagger使用SpringSecurity控制訪問權(quán)限問題
這篇文章主要介紹了SpringBoot集成Swagger使用SpringSecurity控制訪問權(quán)限問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05

