定義
注解(Annotation)也叫元數(shù)據(jù)至壤,它是一種代碼級別的說明,它是JDK 1.5以及以后版本引入的一個特征枢纠,與類像街、接口、枚舉是在同一個層次晋渺,它可以聲明在包镰绎、類、字段木西、方法畴栖、局部變量、方法參數(shù)等的前面八千,用來對這些數(shù)據(jù)進(jìn)行說明吗讶,注釋
功能
- 編寫文檔:通過代碼里標(biāo)識元數(shù)據(jù)生成文檔(javadoc生成java文檔)
- 代碼分析:配合反射獲取注解和相關(guān)參數(shù)(spring 基于注解編程)
- 編譯檢查:override etc ...
Annotation內(nèi)部結(jié)構(gòu)
注解本質(zhì)上是繼承Annotation的一個接口,
元注解
元注解用于定義注解的注解,java中有如下元注解:
- @Target
- @Retention
- @Document
- @Inherited
@Target 元注解用于聲明注解可以使用的作用域恋捆,如果沒有使用@Target照皆,則除了TYPE_PARAMETER,其他地方都可以使用該注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to
*/
ElementType[] value();
}
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
@Retention指定注解的聲明周期沸停,默認(rèn)為CLASS
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}
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
}
@Documented用于聲明是否生成javadoc
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
@Inherited 用于聲明查詢父類的注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}
注解屬性
在注解接口的中定義的方法即為注解屬性其膜毁,返回值有如下要求:
- 基本數(shù)據(jù)類型
- String
- Enum
- Annotation
- 以上類型的數(shù)組
自定義和獲取注解
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnno {
int age();
String name();
//defaut
String desc() default "test";
TestAnno1 anno();
String[] arrays();
}
public @interface TestAnno1 {
}
@TestAnno(age = 1, name = "a", arrays = {"a", "b"}, anno = @TestAnno1)
public class Test {
public static void main(String[] args) {
TestAnno anno = Test.class.getAnnotation(TestAnno.class);
System.out.println("age: " + anno.age());
System.out.println("name: " + anno.name());
System.out.println("arrays: " + Arrays.toString(anno.arrays()));
}
}
運(yùn)行結(jié)果如下:
age: 1
name: a
arrays: [a, b]