1、先看其API定义
(1)Documented:生成API帮助文档时显示注解。
(2)Retention:jvm保留。
(3)Target:此注解是元注解。


3、接下来常见可重复注解,首先创建一个用于存储的容器注解MyRepeatableAnnotations

4、然后创建可重复注解MyRepeatableAnnotation,并声明@Repeatable(MyRepeatableAnnotations.class),此时会报错,且往下看。

5、然后在MyRepeatableAnnotations注解中添加方法:MyRepeatableAnnotation[] value();发现上一步的编译错误没有了。至此可重复注解就完成了

6、编写测试类,编写四个方法,如下图。
只有第四个方法编译器报错了,说明不合法,MyAnnotation不是可重复注解,所以报错。
而第二个方法之所以没有报错是因为,MyRepeatableAnnotation是可重复的注解。

7、编写测试方法,通过反射,获取可重复的注解,发现真的被存储到了MyRepeatableAnnotations中。
1、是获取repeatable2方法的注解,其有两个注解MyRepeatableAnnotation,但是获取出来确是一个MyRepeatableAnnotations;只不过这两个MyRepeatableAnnotation注解被装在了MyRepeatableAnnotations里面,这也就是API中所说的,注解会被存储到容器注解中。
2、是获取1处的注解MyRepeatableAnnotations的值(MyRepeatableAnnotation数组),并输出
3、是获取notRepeatable3方法的注解,就一个MyAnnotation,很正常。


