一、元注解的概念
java中有4個元注解:@retention、@target、@document、@inherited。所謂元注解就是注解的注解。
二、注解介紹
①@retention
@retention——注解的保留位置
注解僅存在于源碼中,在class位元組碼檔案中不包含
@retention(retentionpolicy.source)
預設的保留政策,注解會在class位元組碼檔案中存在,但運作時無法獲得
@retention(retentionpolicy.class)
注解會在class位元組碼檔案中存在,在運作時可以通過反射擷取到
@retention(retentionpolicy.runtime)
②@target
@target——注解的作用目标
接口、類、枚舉、注解
@target(elementtype.type)
字段、枚舉的常量
@target(elementtype.field)
方法
@target(elementtype.method)
方法參數
@target(elementtype.parameter)
構造函數
@target(elementtype.constructor)
局部變量
@target(elementtype.local_variable)
注解
@target(elementtype.annotation_type)
包
@target(elementtype.package)
③@document——說明該注解将被包含在javadoc中
④@inherited——說明子類可以繼承父類中的該注解
三、例子
@target({elementtype.method})
public @interface annatdemo{
}
@annatdemo注解作用目标是用于對方法注解并保留在運作時的環境中,可利用反射獲得一個方法上的注解調用定義的方法。
原帖位址:http://www.cnblogs.com/gordon-yangyibao/archive/2012/08/07/2626340.html