Java通過反射訪問注解信息的方法示例
本文實(shí)例講述了Java通過反射訪問注解信息的方法。分享給大家供大家參考,具體如下:
一 點(diǎn)睛
利用Java的反射機(jī)制,可以訪問注解信息。比如在調(diào)用某個方法時,需要知道該方法的一些基本信息,而這些信息又需要動態(tài)獲取時,利用發(fā)射獲取注解信息是一個比較理想的處理方式。
二 實(shí)戰(zhàn)——訪問類的某個成員方法的注解信息
1 代碼
import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface testAnnoation8 {
public String name() default "methodname";
public String unit() default "unit";
}
public class ch11_8 {
public String aString;
public static void main( String[] args ) {
try {
ch11_8 ch8 = new ch11_8();
Method method = ch8.getClass().getMethod("getData1");
Annotation ans[] = method.getAnnotations();
for (Annotation annotation : ans) {
System.out.println(annotation);
}
Annotation annotation = method.getAnnotation(testAnnoation8.class);
System.out.println(annotation);
} catch (Exception e) {
e.printStackTrace();
}
}
@Deprecated
@testAnnoation8(name = "SOC", unit = "%")
public void getData1() {
}
}
2 運(yùn)行
@java.lang.Deprecated()
@testAnnoation8(name=SOC, unit=%)
@testAnnoation8(name=SOC, unit=%)
三 實(shí)戰(zhàn)——訪問類的某個成員方法的注解信息
1 代碼
import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface testAnnoation9{
public String name() default "methodname";
public String unit() default "unit";
}
public class ch11_9 {
public String aString;
public static void main(String[] args) {
try {
ch11_9 ch9=new ch11_9();
Method method=ch9.getClass().getMethod("getData1");
Annotation annotation=method.getAnnotation(testAnnoation9.class);
testAnnoation9 t9=(testAnnoation9)annotation;
System.out.println("name value is "+t9.name()+"; unit is "+t9.unit());
} catch (Exception e) {
e.printStackTrace();
}
}
@Deprecated
@testAnnoation9(name = "SOC", unit = "%")
public void getData1(){
}
}
2 運(yùn)行
name value is SOC; unit is %
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java面向?qū)ο蟪绦蛟O(shè)計(jì)入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
總結(jié)Junit4,Junit5,Jupiter之間的聯(lián)系
Jupiter和Junit5之間有什么聯(lián)系?Jupiter提供了哪些新的測試方法?如何用IDEA和Jupiter生成可讀性更好的測試報(bào)告?文中有非常詳細(xì)的說明,需要的朋友可以參考下2021-06-06
利用java反射機(jī)制調(diào)用類的私有方法(推薦)
下面小編就為大家?guī)硪黄胘ava反射機(jī)制調(diào)用類的私有方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
SpringBoot中使用Redis?Stream實(shí)現(xiàn)消息監(jiān)聽示例
本文主要介紹了SpringBoot中使用Redis?Stream實(shí)現(xiàn)消息監(jiān)聽示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
SpringBoot自定義注解驗(yàn)證枚舉的實(shí)現(xiàn)
本文主要介紹了SpringBoot自定義注解驗(yàn)證枚舉的實(shí)現(xiàn),數(shù)據(jù)校驗(yàn),需要對枚舉類型的數(shù)據(jù)傳參,進(jìn)行數(shù)據(jù)校驗(yàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
Windows下使用IDEA搭建Hadoop開發(fā)環(huán)境的詳細(xì)方法
這篇文章主要介紹了Windows下使用IDEA搭建Hadoop開發(fā)環(huán)境,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
SpringBoot實(shí)現(xiàn)海量數(shù)據(jù)高效實(shí)時搜索功能
我們都知道隨著業(yè)務(wù)系統(tǒng)的發(fā)展和使用,數(shù)據(jù)庫存儲的業(yè)務(wù)數(shù)據(jù)量會越來越大,逐漸成為了業(yè)務(wù)系統(tǒng)的瓶頸,本文給大家介紹了Spring Boot業(yè)務(wù)系統(tǒng)如何實(shí)現(xiàn)海量數(shù)據(jù)高效實(shí)時搜索,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-10-10

