@Retention元註解的使用
阿新 • • 發佈:2019-01-14
@Retention註解標記其他的註解用於指明標記的註解保留策略:
先看Java SE 8中@Target是如何宣告的:
package java.lang.annotation; public enum RetentionPolicy { /** * Annotations are to be discarded by the compiler. */ SOURCE, /** * Annotations are to be recorded in the class file by the compiler * but need not be retained by the VM at run time. This is the default * behavior.*/ CLASS, /** * Annotations are to be recorded in the class file by the compiler and * retained by the VM at run time, so they may be read reflectively. * * @see java.lang.reflect.AnnotatedElement */ RUNTIME }
從原始碼的註釋中,我們看到java.lang.annotation.RetentionPolicy此列舉類聲明瞭三種保留策略:
java.lang.annotation.RetentionPolicy.SOURCE:表示註解會在編譯時被丟棄
java.lang.annotation.RetentionPolicy.CLASS:預設策略,表示註解會在編譯後的class檔案中存在,但是在執行時,不會被VM保留。
java.lang.annotation.RetentionPolicy.RUNTIME:表示不僅會在編譯後的class檔案中存在,而且在執行時保留,因此它們主要用於反射場景,可以通過getAnnotation方法獲取。
這三種保留策略的使用示例:
宣告@Country註解,採用的是RUNTIME策略:
package org.springmorning.demo.javabase.annotation.meta; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author 春晨 * @date 2019/1/14 19:30 * Copyright ©2019 春晨 https://www.cnblogs.com/springmorning/p/10265030.html */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface Country { //國家名稱 String name(); //國家語言 String[] languages(); }
宣告@Region註解,採用的是CLASS策略(預設策略):
package org.springmorning.demo.javabase.annotation.meta; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author 春晨 * @date 2019/1/14 19:37 * Copyright ©2019 春晨 https://www.cnblogs.com/springmorning/p/10265030.html */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.CLASS) public @interface Region { //地區名稱 String name(); //所屬國家 String country(); }
宣告@Home註解,採用的是SOURCE策略:
package org.springmorning.demo.javabase.annotation.meta; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author 春晨 * @date 2019/1/14 19:43 * Copyright ©2019 春晨 https://www.cnblogs.com/springmorning/p/10265030.html */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.SOURCE) public @interface Home { //成員 String[] members(); //地址 String address(); }
編寫測試類:
package org.springmorning.demo.javabase.annotation.meta; import java.lang.annotation.Annotation; /** * @author 春晨 * @date 2019/1/14 19:34 * Copyright ©2019 春晨 https://www.cnblogs.com/springmorning/p/10265030.html */ @Country( name = "China", languages = {"Chinese"} ) @Region( name = "GuangDong", country = "China" ) @Home( members = {"Wolffy","Wolnie","Wilie"}, address = "Qingqing grasslands" ) public class RetentionAnnotation { public static void main(String[] args) { Annotation[] annotations = RetentionAnnotation.class.getAnnotations(); System.out.println("獲取能保留到執行時的註解:"); for (Annotation annotation :annotations){ System.out.println(annotation.toString()); } } }
執行結果:
獲取能保留到執行時的註解:
@org.springmorning.demo.javabase.annotation.meta.Country(name=China, languages=[Chinese])
採用javap命令分析RetentionAnnotation的class檔案如下圖:
下節繼續
下節將給大家講解元註解@Document的使用。