在jdk1.8之前,自定义注解如果需要多次引用,一般都采用封装模式,即把一个自定义注解作为参数封装进另一个自定义注解,达到可以对同一个类多次注解的目的。但是这种注解方式还很有局限性
例:
@Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface A { String id ; }
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface B {
A[] value() ;
}
在使用时只能
@B({@a(id="a"),@a(id="b"),@a(id="c")})
不处理这种情况
@B({@a(id="a"),@a(id="b"),@a(id="c")})
@B({@a(id="d"),@a(id="e"),@a(id="f")})
所以在jdk1.8,出现了@repeatable注解,
使用方法:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@repeatable(A.class)
public @interface A {
String id ;
}
在使用A注解时
@A(id="a")
@A(id="b")
public test{
}
以上例子,纯属方便理解,无任何意义
就到这类了,再见了