首先什么是注解?
最常見的是,在我們使用Eclipse等工具編寫java代碼的時候,有時候會出現一些比如@Deprecated,@Override,@SuppressWarnings等東東。這個就是常見的幾種注解。
在開發Java程序,尤其是Java EE應用的時候,總是免不了與各種配置文件打交道。以Java EE中典型的S(pring)S(truts)H(ibernate)架構來說,Spring、Struts和Hibernate這三個框架都有自己的XML格式的配置文件。這些配置文件需要與Java源代碼保存同步,否則的話就可能出現錯誤。而且這些錯誤有可能到了運行時刻才被發現。把同一份信息保存在兩個地方,總是個壞的主意。理想的情況是在一個地方維護這些信息就好了。其它部分所需的信息則通過自動的方式來生成。JDK 5中引入了源代碼中的注解(annotation)這一機制。注解使得Java源代碼中不但可以包含功能性的實現代碼,還可以添加元數據。注解的功能類似於代碼中的注釋,所不同的是注解不是提供代碼功能的說明,而是實現程序功能的重要組成部分。Java注解已經在很多框架中得到了廣泛的使用,用來簡化程序中的配置。
而我們最常見的可能就是上面提到的這三個注解了,簡單的介紹一下上面的這三個注解的作用:
@Override:只能用在方法之上的,用來告訴別人這一個方法是改寫父類的。
@Deprecated:建議別人不要使用舊的API的時候用的,編譯的時候會用產生警告信息,可以設定在程序里的所有的元素上.
@SuppressWarnings:這一個類型可以來暫時把一些警告信息消息關閉.
在jdk自帶的java.lang.annotation包里,打開如下幾個源文件:Target.java,Retention.java,RetentionPolicy.java,ElementType.java。
內容分別為:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}
public enum RetentionPolicy {
SOURCE,
CLASS,
RUNTIME
}
public enum ElementType {
TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR,
LOCAL_VARIABLE, ANNOTATION_TYPE,PACKAGE
}
在設計annotations的時候必須把一個類型定義為@interface。
我們需要注意的是:SOURCE,CLASS 和 RUNTIME.這三個級別。
SOURCE代表的是這個Annotation類型的信息只會保留在程序源碼里,源碼如果經過了編譯之后,Annotation的數據就會消失,並不會保留在編譯好的.class文件里面。
ClASS的意思是這個Annotation類型的信息保留在程序源碼里,同時也會保留在編譯好的.class文件里面,在執行的時候,並不會把這一些信息加載到虛擬機(JVM)中去.注意一下,當你沒有設定一個Annotation類型的Retention值時,系統默認值是CLASS.
RUNTIME,表示在源碼、編譯好的.class文件中保留信息,在執行的時候會把這一些信息加載到JVM中去的.
@Target里面的ElementType是用來指定Annotation類型可以用在哪一些元素上的.說明一下:TYPE(類型), FIELD(屬性), METHOD(方法), PARAMETER(參數), CONSTRUCTOR(構造函數),LOCAL_VARIABLE(局部變量), ANNOTATION_TYPE,PACKAGE(包),其中的TYPE(類型)是指可以用在Class,Interface,Enum和Annotation類型上.
另外,@Target自己也用了自己來聲明自己。如果一個Annotation類型沒有指明@Target使用在哪些元素上,那么它可以使用在任何元素之上,這里的元素指的是上面的八種類型.
舉幾個正確的例子:
@Target(ElementType.METHOD) @Target(value=ElementType.METHOD) @Target(ElementType.METHOD,ElementType.CONSTRUCTOR)
@Documented的目的就是讓這一個Annotation類型的信息能夠顯示在javaAPI說明文檔上;沒有添加的話,使用javadoc生成API文檔的時候就會找不到這一個類型生成的信息.
另外一點,如果需要把Annotation的數據繼承給子類,那么就會用到@Inherited這一個Annotation類型.
本文只是簡單的說了一下注解的常規用法,至於更加深入的注解學習,請參見文章末尾的參考資料。下面我們來看自定義一個注解:源代碼有如下幾個:

源碼分別為:
package com.java.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 類注解
* */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyAnnotationClass {
public String msg();
}
package com.java.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 方法注解
* */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotationMethod {
public String common();
}
package com.java.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface MyAnnotationField {
boolean request();
}
package com.java.annotation;
@MyAnnotationClass(msg = "這個是一個類注解")
public class MyAnnotationDemo {
public MyAnnotationDemo() {
}
public MyAnnotationDemo(String hello) {
this.hello = hello;
}
@MyAnnotationMethod(common = "這個是一個方法注解")
public void method() {
System.out.println(hello);
}
@MyAnnotationField(request = true)
private String hello;
}
package com.java.annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class MyAnnotationTest {
public static void main(String[] args) {
MyAnnotationDemo demo = new MyAnnotationDemo("hello rollen");
MyAnnotationClass annotationClass = demo.getClass().getAnnotation(MyAnnotationClass.class);
System.out.println(annotationClass.msg());
Method method = null;
try {
method = demo.getClass().getMethod("method",new Class[0]);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
MyAnnotationMethod annotationMethod = method.getAnnotation(MyAnnotationMethod.class);
System.out.println(annotationMethod.common());
Field field = null;
try {
field = demo.getClass().getDeclaredField("hello");
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
MyAnnotationField annotationField = field.getAnnotation(MyAnnotationField.class);
System.out.println(annotationField.request());
}
}
參考資料:http://www.infoq.com/cn/articles/cf-java-annotation
http://tiantian0521.blog.163.com/blog/static/4172088320118243436208/
