本篇文章為大家展示了怎么在Java中利用反射訪問注解信息例,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)主要從事成都網(wǎng)站建設(shè)、成都做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)望謨,十多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
利用Java的反射機(jī)制,可以訪問注解信息。比如在調(diào)用某個(gè)方法時(shí),需要知道該方法的一些基本信息,而這些信息又需要?jiǎng)討B(tài)獲取時(shí),利用發(fā)射獲取注解信息是一個(gè)比較理想的處理方式。
二 實(shí)戰(zhàn)——訪問類的某個(gè)成員方法的注解信息
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 ch21_8 {
public String aString;
public static void main( String[] args ) {
try {
ch21_8 ch8 = new ch21_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)——訪問類的某個(gè)成員方法的注解信息
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 ch21_9 {
public String aString;
public static void main(String[] args) {
try {
ch21_9 ch9=new ch21_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 %
上述內(nèi)容就是怎么在Java中利用反射訪問注解信息例,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)頁名稱:怎么在Java中利用反射訪問注解信息例
文章地址:http://chinadenli.net/article0/ipgdio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、虛擬主機(jī)、網(wǎng)站排名、網(wǎng)站營銷、小程序開發(fā)、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)