Java 自定義注解及利用反射讀取注解的實(shí)例
一、自定義注解
元注解:
@interface注解: 定義注解接口
@Target注解: 用于約束被描述的注解的使用范圍,當(dāng)被描述的注解超出使用范圍則編譯失敗。如:ElementType.METHOD,ElementType.TYPE;
@Retention 注解:用于約束被定義注解的作用范圍,作用范圍有三個(gè):
1、RetentionPolicy.SOURCE:作用范圍是源碼,作用于Java文件中,當(dāng)執(zhí)行javac時(shí)去除該注解。
2、RetentionPolicy.CLASS:作用范圍是二進(jìn)制碼,就是存在于class文件中,當(dāng)執(zhí)行Java時(shí)去除該注解。
3、RetentionPolicy.RUNTIME:作用范圍為運(yùn)行時(shí),就是我們可以通過(guò)動(dòng)態(tài)獲取該注釋。
@Documented:用于指定javadoc生成API文檔時(shí)顯示該注釋。
@Inherited:用于指定被描述的注釋可以被其描述的類的子類繼承,默認(rèn)情況是不能被其子類繼承。
自定義注解接口:
package com.java.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.TYPE})
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation_my {
String name() default "張三";//defalt 表示默認(rèn)值
String say() default "hello world";
int age() default 21;
}
接下來(lái)我們定義一個(gè)接口:
package com.java.annotation;
@Annotation_my //使用我們剛才定義的注解
public interface Person {
@Annotation_my
public void name();
@Annotation_my
public void say();
@Annotation_my
public void age();
}
接口定義好了,我們就可以寫(xiě)接口的實(shí)現(xiàn)類了(接口不能實(shí)例化)
package com.java.annotation;
@Annotation_my
@SuppressWarnings("unused")
public class Student implements Person {
private String name;
@Override
@Annotation_my(name="流氓公子") //賦值給name 默認(rèn)的為張三
//在定義注解時(shí)沒(méi)有給定默認(rèn)值時(shí),在此處必須name賦初值
public void name() {
}
@Override
@Annotation_my(say=" hello world !")
public void say() {
}
@Override
@Annotation_my(age=20)
public void age() {
}
}
然后我們就編寫(xiě)一個(gè)測(cè)試類測(cè)試我們的注解
package com.java.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Text {
Annotation[] annotation = null;
public static void main(String[] args) throws ClassNotFoundException {
new Text().getAnnotation();
}
public void getAnnotation() throws ClassNotFoundException{
Class<?> stu = Class.forName("com.java.annotation.Student");//靜態(tài)加載類
boolean isEmpty = stu.isAnnotationPresent(com.java.annotation.Annotation_my.class);//判斷stu是不是使用了我們剛才定義的注解接口if(isEmpty){
annotation = stu.getAnnotations();//獲取注解接口中的
for(Annotation a:annotation){
Annotation_my my = (Annotation_my)a;//強(qiáng)制轉(zhuǎn)換成Annotation_my類型
System.out.println(stu+":\n"+my.name()+" say: "+my.say()+" my age: "+my.age());
}
}
Method[] method = stu.getMethods();//
System.out.println("Method");
for(Method m:method){
boolean ismEmpty = m.isAnnotationPresent(com.java.annotation.Annotation_my.class);
if(ismEmpty){
Annotation[] aa = m.getAnnotations();
for(Annotation a:aa){
Annotation_my an = (Annotation_my)a;
System.out.println(m+":\n"+an.name()+" say: "+an.say()+" my age: "+an.age());
}
}
}
//get Fields by force
System.out.println("get Fileds by force !");
Field[] field = stu.getDeclaredFields();
for(Field f:field){
f.setAccessible(true);
System.out.println(f.getName());
}
System.out.println("get methods in interfaces !");
Class<?> interfaces[] = stu.getInterfaces();
for(Class<?> c:interfaces){
Method[] imethod = c.getMethods();
for(Method m:imethod){
System.out.println(m.getName());
}
}
}
}
以上這篇Java 自定義注解及利用反射讀取注解的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Guava?Retryer實(shí)現(xiàn)接口重試的示例
本文主要介紹了Guava?Retryer實(shí)現(xiàn)接口重試的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Quarkus改造Pmml模型項(xiàng)目異常記錄及解決處理
這篇文章主要為大家介紹了Quarkus改造Pmml模型項(xiàng)目是遇到的異常記錄以及解決方法,有需要的同學(xué)可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02
基于SpringBoot解決CORS跨域的問(wèn)題(@CrossOrigin)
這篇文章主要介紹了基于SpringBoot解決CORS跨域的問(wèn)題(@CrossOrigin),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
Java實(shí)現(xiàn)文件壓縮為zip和解壓zip壓縮包
這篇文章主要為大家介紹了Java如何實(shí)現(xiàn)將文件壓縮為zip以及解壓zip壓縮包,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-06-06
如何構(gòu)建可重復(fù)讀取inputStream的request
這篇文章主要介紹了如何構(gòu)建可重復(fù)讀取inputStream的request,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot項(xiàng)目War包部署無(wú)法注冊(cè)到Nacos中的解決
這篇文章主要介紹了SpringBoot項(xiàng)目War包部署無(wú)法注冊(cè)到Nacos中的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

