@Documented
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Repeatable(AAA.list.class)
public @interface AAA {
@AliasFor("name")
String value() default "456";
@AliasFor("value")
String name() default "456";
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@interface list {
AAA[] value();
}
}
@Documented
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@AAA
public @interface BBB {
@AliasFor(annotation = AAA.class)
String value() default "456";
}
1、单独使用 @AAA 时,需要 @AliasFor 生效,使用以下代码
// @AAA("123")
// public void test06() throws NoSuchMethodException {
AnnotationUtils.findAnnotation(method, AAA.class)
2、单独使用 @BBB 时,需要取到 @AAA 注解,使用以下代码
// @BBB("123")
// public void test06() throws NoSuchMethodException {
AnnotatedElementUtils.findMergedAnnotation(method, AAA.class)
// 或
AnnotatedElementUtils.findMergedRepeatableAnnotations(method, AAA.class)
3、多个 @AAA 和 @BBB 同时使用时,获取所有 @AAA 注解,使用以下代码
// @AAA("111")
// @BBB("123")
// public void test06() throws NoSuchMethodException {
AnnotatedElementUtils.findMergedRepeatableAnnotations(method, AAA.class)
默认可以使用第三种