注解(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 程序员的开发工作。正因如此,我们不仅需要了解常用的注解使用方式,还需要深入理解注解背后的工作原理。