JAVA注解学习-@Repeatable注解

@Repeatable注解是用来标注一个注解在同一个地方可重复使用的一个注解,比如说你定义了一个注解,如果你的注解没有标记@Repeatable这个JDK自带的注解,那么你这个注解在引用的地方就只能使用一次。
例:
在这里插入图片描述
这里@ImportResource(value = “”)注解里面没有设置添加@Repeatable属性,所有引入的多次的时候就会报错,在这里插入图片描述

参考链接

java8注解@Repeatable使用技巧
Java @Repeatable

代码演示

简单定义一个注解

定义一个最简单的API注解,只有一个属性content()

package com.example.demo.anno;
import java.lang.annotation.Repeatable;

public @interface API {
    String content() default "请添加描述";
}

新建一个类来引入注解

新建了一个Student对象,在类上面引用了API注解,这里只引用了一次,所以编译没有问题,因为我没有指定API的使用位置,这里随便放在类上,方法上都可以。
在这里插入图片描述
当我引用多次的时候编译就报错了,看报错上面的提示是说这个注解重复使用了,并且没有有效的@Repeatable注解,然后我们按照提示添加一下@Repeatable注解。
在这里插入图片描述

@Repeatable注解的源码

这个注解是在jdk1.8以后添加的,里面只有一个属性Class,但是这个Class必须是继承了Annotation(这里其应该是说Class的值必须也是一个注解),

package java.lang.annotation;

/**
 * The annotation type {@code java.lang.annotation.Repeatable} is
 * used to indicate that the annotation type whose declaration it
 * (meta-)annotates is <em>repeatable</em>. The value of
 * {@code @Repeatable} indicates the <em>containing annotation
 * type</em> for the repeatable annotation type.
 *
 * @since 1.8
 * @jls 9.6 Annotation Types
 * @jls 9.7 Annotations
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Repeatable {
    /**
     * Indicates the <em>containing annotation type</em> for the
     * repeatable annotation type.
     * @return the containing annotation type
     */
    Class<? extends Annotation> value();
}

当引入@Repeatable的时候必须需要指定它的value值。
在这里插入图片描述
这里新建了一个APIS的一个新注解,里面定义的是一API注解的数组。
在这里插入图片描述
APIS里面定义了一个API的注解数组,这里面的属性类型必须是API注解,因为你是需要在API注解里面引用这个APIS的注解,只有一致才能正常引用。
在这里插入图片描述

代码

package com.example.demo.anno;

import java.lang.annotation.Repeatable;

@Repeatable(APIS.class)
public @interface API {

    String content() default "请添加描述";
}
------------------------------------------

package com.example.demo.anno;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface APIS {

    API[] value();
}
------------------------------------------

package com.example.demo.annotest;

import com.example.demo.anno.API;

@API(content = "学生对象1")
@API(content = "学生对象2")
@API(content = "学生对象3")
public class Student {

}
------------------------------------------
package com.example.demo.annotest;

import com.example.demo.anno.API;
import com.example.demo.anno.APIS;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedType;

public class TestAnno {

    public static void main(String[] args) {
        Annotation[] annotations = Student.class.getAnnotations();
        for (Annotation annotation : annotations) {

            System.out.println(annotation.annotationType().getName());
            APIS api = (APIS) annotation;
            for (API a : api.value()) {
                System.out.println(a.content());
            }
        }
        APIS annotation = Student.class.getAnnotation(APIS.class);
        System.out.println(annotation.value()[0].content());
        AnnotatedType[] annotatedInterfaces = Student.class.getAnnotatedInterfaces();
        for (AnnotatedType annotatedInterface : annotatedInterfaces) {
            System.out.println(annotatedInterface.getType());
        }
        Student.class.getComponentType();
    }

}

总结

@Repeatable注解用来标注另外一个注解是否可以引用多次。

发布了188 篇原创文章 · 获赞 34 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/u010316188/article/details/103651646