詳解Java注解的實現(xiàn)與使用方法
詳解Java注解的實現(xiàn)與使用方法
Java注解是java5版本發(fā)布的,其作用就是節(jié)省配置文件,增強代碼可讀性。在如今各種框架及開發(fā)中非常常見,特此說明一下。
如何創(chuàng)建一個注解
每一個自定義的注解都由四個元注解組成,這四個元注解由java本身提供:
@Target(ElementType.**)
這是一個枚舉,它置頂是該自定義的注解使用的地方,像類、變量、方法等
@Retention(RetentionPolicy.**)作用是標明注解保存在什么級別,像在編譯時、class文件中,vm運行中
@Documented 將此注解包含在 javadoc 中 ,它代表著此注解會被javadoc工具提取成文檔。在doc文檔中的內(nèi)容會因為此注解的信息內(nèi)容不同而不同
@Inherited : 在您定義注解后并使用于程序代碼上時,預設上父類別中的注解并不會被繼承至子類別中,您可以在定義注解時加上java.lang.annotation.Inherited 限定的Annotation,這讓您定義的Annotation型別被繼承下來。
介紹完理論,開始代碼(talk is cheap,show your code)
package com.yasin.JavaLearn; 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; /** * 這是一個類級別的注釋,這個注釋中有一個name字段,默認值是 yasin * @author yasin * */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Learn { String name() default "yasin"; }
package com.yasin.JavaLearn; 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; /** * 這是一個變量級別的注解,注解中有一個字段name,默認值是field * @author yasin * */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface FiledLearn { String name() default "field"; }
package com.yasin.JavaLearn; 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; /** * 這是一個方法級別的注解 * @author yasin * */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface MethodLearn { String name() default "method"; }
上面了我定義了三個注解,分別是常用的類、變量、方法三個級別的注解。
下面我定義一個類,使用這三個注解
package com.yasin.JavaLearn; @Learn public class Yasin { @FiledLearn public int level; @FiledLearn(name="xq") public String xq; public String a; @MethodLearn(name="test") public void setMain(){ } public void setA(){ } }
下面就是如何使用這個注解了,注解的提取,都是通過class反射得到相應的變量和方法,在從變量和方法中獲得注解。
package com.yasin.JavaLearn; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.xml.DOMConfigurator; /** * Hello world! * */ public class App { public static void main(String[] args) { Learn learn = Yasin.class.getAnnotation(Learn.class); System.out.println(learn.name()); Field[] fields = Yasin.class.getFields();//獲取該類所有的字段 for(Field filed:fields){ if(filed.isAnnotationPresent(FiledLearn.class)){//校驗該字段是否添加這個注解 System.out.println(filed.getName()); FiledLearn filedLearn = filed.getAnnotation(FiledLearn.class); System.out.println(filedLearn.name()); } } Method[] methods = Yasin.class.getMethods(); for(Method method:methods){ if(method.isAnnotationPresent(MethodLearn.class)){//校驗該方法是否有這個注解 System.out.println(method.getName()); MethodLearn methodLearn = method.getAnnotation(MethodLearn.class); System.out.println(methodLearn.name()); } } } }
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
IntelliJ?IDEA?2021.3永久最新激活至2099年(親測有效)
最新版idea2021.3已出來,很多網(wǎng)友迫不及待的要升級idea2021最新版,今天小編抽空給大家整理了一篇教程關于idea2021.3最新激活教程,本文以idea2021.2.3為例通過圖文并茂的形式給大家分享激活詳細過程,感興趣的朋友參考下吧2020-12-12Java中List.of()和Arrays.asList()的區(qū)別及原因分析
這篇文章主要介紹了Java中List.of()和Arrays.asList()的區(qū)別及原因分析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09springmvc中RequestMappingHandlerAdapter與HttpMessageConverter的
今天小編就為大家分享一篇關于springmvc中RequestMappingHandlerAdapter與HttpMessageConverter的裝配講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01關于Java單個TCP(Socket)連接發(fā)送多個文件的問題
這篇文章主要介紹了關于Java單個TCP(Socket)連接發(fā)送多個文件的問題,每次我只能使用一個 Socket 發(fā)送一個文件,沒有辦法做到連續(xù)發(fā)送文件,本文來解決這個問題,需要的朋友可以參考下2023-04-04Java concurrency之AtomicReference原子類_動力節(jié)點Java學院整理
AtomicReference是作用是對"對象"進行原子操作。這篇文章主要介紹了Java concurrency之AtomicReference原子類,需要的朋友可以參考下2017-06-06