Java在1.5開始引入了注解,目前流行的框架都在用注解,可想而知注解的強(qiáng)大之處。
東鄉(xiāng)網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站設(shè)計等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)公司公司2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
以下通過自定義注解來深入了解java注解。
一、創(chuàng)建自定義注解
package com.sam.annotation; import java.lang.annotation.*; /** * @author sam * @since 2017/7/13 */ @Target({ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented public @interface MyMessage { String name() default "sam"; int num() default 0; String desc(); }
說明:
@Target、@Retention、@Inherited、@Documented為元注解(meta-annotation),它們是負(fù)責(zé)注解其他注解的。
1、Target:指明注解支持的使用范圍,取值可以參考枚舉ElementType,以下:
ElementType.TYPE //類、接口、枚舉
ElementType.FIELD //屬性
ElementType.METHOD //方法
ElementType.PARAMETER //參數(shù)
ElementType.CONSTRUCTOR //構(gòu)造器
ElementType.LOCAL_VARIABLE //局部變量
ElementType.ANNOTATION_TYPE //注解
ElementType.PACKAGE //包
2、Retention:指明注解保留的的時間長短,取值參考枚舉RetentionPolicy,一下:
SOURCE //源文件中保留
CLASS //class編譯時保留
RUNTIME //運(yùn)行時保留
3、Inherited:指明該注解類型被自動繼承。如果一個annotation注解被@Inherited修飾,那么該注解作用于的類 的子類也會使用該annotation注解。
4、Documented:指明擁有這個注解的元素可以被javadoc此類的工具文檔化。
二、創(chuàng)建測試類,使用自定義注解
package com.sam.annotation; /** * @author sam * @since 2017/7/13 */ public class AnnotationTest { @MyMessage(num = 10, desc = "參數(shù)a") private static int a; @MyMessage(name = "Sam test", desc = "測試方法test") public void test() { System.out.println("test"); } }
在該類中的屬性和方法,使用了自定義的注解,并指明了參數(shù)。
那么現(xiàn)在就需要解析自定義的注解。
三、解析注解
使用反射機(jī)制處理自定義注解
package com.sam.annotation; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * 使用反射處理注解 * * @author sam * @since 2017/7/13 */ public class MyMessageProcessor { public static void main(String[] args) { try { //加載annotationTest.class類 Class clazz = MyMessageProcessor.class.getClassLoader().loadClass("com.sam.annotation.AnnotationTest"); //獲取屬性 Field[] fields = clazz.getDeclaredFields(); //遍歷屬性 for (Field field : fields) { MyMessage myMessage = field.getAnnotation(MyMessage.class); System.out.println("name:" + myMessage.name() + " num:" + myMessage.num() + " desc:" + myMessage.desc()); } //獲取類中的方法 Method[] methods = clazz.getMethods(); //遍歷方法 for (Method method : methods) { //判斷方法是否帶有MyMessage注解 if (method.isAnnotationPresent(MyMessage.class)) { // 獲取所有注解 method.getDeclaredAnnotations(); // 獲取MyMessage注解 MyMessage myMessage = method.getAnnotation(MyMessage.class); System.out.println("name:" + myMessage.name() + " num:" + myMessage.num() + " desc:" + myMessage.desc()); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
運(yùn)行MyMessageProcessor 得到結(jié)果:
name:sam num:10 desc:參數(shù)a name:Sam test num:0 desc:測試方法test Process finished with exit code 0
具體定制注解所實(shí)現(xiàn)的內(nèi)容,可以在MyMessageProcessor.java中進(jìn)行修改。
自此,已經(jīng)對java的自定義注解有簡單的了解。
以上這篇簡單談?wù)刯ava自定義注解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持創(chuàng)新互聯(lián)。