注解(Annotation)是 Java 中的一個類型,通俗地理解就像一個標簽,貼在了代碼上。眾所周知,Spring 支持大量注解,基于注解可以完成 Bean 的注入和生命周期管理,注解也是取代 xml 配置的一種方法。使用注解可以把元數據和源代碼綁定在一起,可以用于描述代碼無法描述的信息,并在編譯或運行中使用。
@Service @MyAnnotation public class TestAnnotation implements ApplicationContextAware { ApplicationContext context; //定義一個注解 @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation{} @Override public void setApplicationContext(ApplicationContext context) throws BeansException { this.context = context; } @PostConstruct private void init(){ Map<String, Object> beans = context.getBeansWithAnnotation(MyAnnotation.class); System.out.println("找到 bean " + beans.keySet()); } }
定義注解
定義注解非常像定義接口,在 interface 關鍵詞前面加了一個 @。實際上,注解類型與其他類型一樣,終也是編譯成 class 文件。定義注解需要用到一些元注解,這些元注解是 Java 語言提供的,用來定義注解的一些特性。
元注解 @Inherited 用于聲明該注解支持子類繼承父類中的注解。
元注解 @Documented 用于聲明該注解需要包含到 Javadoc 中。
元注解 @Retention 定義注解的級別,可選值定義在 RetentionPolicy 枚舉類,包括運行時(RUNTIME)、類文件(CLASS)、源代碼(SOURCE)。
元注解 @Target 定義注解的使用范圍,值的類型是 ElementType 枚舉值。注解的使用范圍可以設置一個或多個,常用的有 ElementType.TYPE 表示用于 Java 類、ElementType.METHOD 表示用于 Java 方法、ElementType.FIELD 表示用于類的字段、ElementType.PARAMETER 表示用于方法的參數。
注解支持定義元素,定義的形式有點類似方法定義,并可以使用 default 設置默認值。如下代碼中定義的值為 value,獲取值調用 value() 即可,如果使用時沒有給出值則默認值為 test。需要注意的是注解的默認值不能是 null,終一定會有一個值。如果需要表示值不存在,需要自己定義一個特殊值來表示。
注解的元素類型支持基本類型、String、Class、enum、Annotation 以及這些類型的數組。
//定義一個注解 @Inherited @Documented @Target({ElementType.TYPE,ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation2 { String value() default "test"; }
從 Java 8 開始,新增了元注解 @Repeatable,用于表示注解可以有多個值。如下的代碼 SingleAnnotation 可以有多個值,使用 @Repeatable 聲明了多個值合并為注解 @RepeatAnnotation。這種情況下,@RepeatAnnotation 的元素必須有 value,且類型必須為 SingleAnnotation 數組,才能進行聚合。
使用時,@SingleAnnotation 可以定義多次給出多個不同的值,解析注解時則需要按照 @RepeatAnnotation 進行解析。關于注解的解析,請參考后文注解處理器。
@Retention(RetentionPolicy.RUNTIME) @interface RepeatAnnotation { SingleAnnotation[] value(); } @Retention(RetentionPolicy.RUNTIME) @Repeatable(RepeatAnnotation.class) @interface SingleAnnotation{ String value(); } @SingleAnnotation("hello") @SingleAnnotation("world") public class RepeatAnnotationTest{ public static void main(String[] args){ RepeatAnnotation annotation = RepeatAnnotationTest.class.getAnnotation(RepeatAnnotation.class); for (SingleAnnotation singleAnnotation : annotation.value()) { System.out.println(singleAnnotation.value()); } } }
Java 中的注解不支持繼承,因此不能使用 extends 關鍵字。但是注解可以進行嵌套,也就是一個注解的值是另一個注解。如下的代碼中,MyAnnotation3 注解的第二個值類型為 MyAnnotation2,使用時對第二個值賦值一樣需要按照注解的格式寫為 @MyAnnotation2("world")。
public @interface MyAnnotation3 { String value() default "test"; MyAnnotation2 annotation(); } @MyAnnotation3(value = "hello" ,annotation = @MyAnnotation2("world")) private void test2(){}
注解處理器
定義注解以后需要編寫注解處理器,才能發揮注解的作用。在開始的例子中,我們定義的注解處理器從 Spring 上下文中找出所有使用了注解 MyAnnotation 的 Java Bean,然后打印出這些 Bean 的名字。
@PostConstruct private void init(){ Map<String, Object> beans = context.getBeansWithAnnotation(MyAnnotation.class); System.out.println("找到 bean " + beans.keySet()); }
在 Java 中可以通過反射很方便地處理注解。對于 Class 上面的注解,可以通過 Class 的方法獲取所有注解或指定注解,如下所示。
//獲取類型的所有注解
TestAnnotation.class.getAnnotations();
TestAnnotation.class.getDeclaredAnnotations();
//獲取類型的指定注解
TestAnnotation.class.getAnnotation(MyAnnotation.class);
TestAnnotation.class.getDeclaredAnnotation(MyAnnotation.class);
對于方法上的注解,可以通過 Method 的方法獲取所有注解或指定注解,如下所示。Method 還提供了 getParameterAnnotations() 來獲取方法參數上面的所有注解,每個參數的注解都是一個數組,返回值為二維數組。
Method init = TestAnnotation.class.getDeclaredMethod("init");
//獲取方法的所有注解
init.getAnnotations();
init.getDeclaredAnnotations();
//獲取方法的指定注解
init.getAnnotation(PostConstruct.class);
init.getDeclaredAnnotation(PostConstruct.class);
//獲取方法的參數的所有注解,返回二維數組
init.getParameterAnnotations();
對注解進行處理時,注解可以當做普通的變量對待。如下代碼所示,annotation 是一個注解類型的變量,與普通變量區別不大,可以有類型定義,值也可以是 null。但是讀取注解的值時,需要調用方法,如 annotation.value()。
MyAnnotation2 annotation = TestAnnotation.class.getAnnotation(MyAnnotation2.class); if(annotation != null){ System.out.println(annotation.value()); }
結語
關于 Java 注解,本文只覆蓋這些內容。注解是一個被廣泛使用的特性,如 Java 本身的 @Override、Spring 中的 @Service @Autowired、MyBatis 中的 @Insert @Update 等。基于注解,實現了對代碼的自動化處理,如掃描和自動注入 JavaBean,大大簡化了 Java 程序員的開發工作。正因如此,我們不僅需要了解常用的注解使用方式,還需要深入理解注解背后的工作原理。