1. 概述1.2 简介Java 8 对注解处理提供了两点改进,可重复的注解及可用于类型的注解2. 重复注解要想定义重复注解,必须给它定义的容器类,还要使用 @Repeatable 注解修饰一下@Repeatable(RepetitionAnnotations.class)@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.RUNTIME)public @interface RepetitionAnnotation { String value() default “ling”;}/** * 容器类 */@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})@Retention(RetentionPolicy.RUNTIME)public @interface RepetitionAnnotations { RepetitionAnnotation[] value();}测试方法public class AnnotationTest { @Test public void t1() throws Exception { Class<AnnotationTest> clazz = AnnotationTest.class; Method method = clazz.getMethod(“show”); // 获取方法上的注解 RepetitionAnnotation[] ras = method.getAnnotationsByType(RepetitionAnnotation.class); for (RepetitionAnnotation repetitionAnnotation : ras) { System.out.println(repetitionAnnotation.value()); } } @RepetitionAnnotation(“Hello”) @RepetitionAnnotation(“World”) public void show() { }}3. 类型注解就是向 @Target 添加一种类型 TYPE_PARAMETER@Repeatable(RepetitionAnnotations.class)@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER})@Retention(RetentionPolicy.RUNTIME)public @interface RepetitionAnnotation { String value() default “ling”;}使用@RepetitionAnnotation(“Hello”)@RepetitionAnnotation(“World”)public void show(@RepetitionAnnotation String str) {}本文首发于凌风博客:Java 8 重复注解与类型注解作者:凌风