最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java注解之Repeatable解讀

 更新時間:2023年06月15日 16:57:31   作者:蠟筆沒了小新git  
這篇文章主要介紹了Java注解之Repeatable,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Java注解之Repeatable

Repeatable使用場景

在需要對同一種注解多次使用時,往往需要借助@Repeatable。

實(shí)例

在生活中一個人往往是具有多種身份,如果我把每種身份當(dāng)成一種注解該如何使用???

先聲明一個Persons類用來包含所有的身份

@Target(ElementType.TYPE) ?
@Retention(RetentionPolicy.RUNTIME)
public ? @interface Persons {
?? ?Person[] value();
}

這里@Target是聲明Persons注解的作用范圍,參數(shù)ElementType.Type代表可以給一個類型進(jìn)行注解,比如類,接口,枚舉,注解。

@Retention是注解的有效時間,RetentionPolicy.RUNTIME是指程序運(yùn)行的時候。

Person注解:

@Repeatable(Persons.class)
public ?@interface Person{
?? ?String role() default "";
}

@Repeatable括號內(nèi)的就相當(dāng)于用來保存該注解內(nèi)容的容器。

聲明一個Man類,給該類加上一些身份。

@Person(role="CEO")
@Person(role="husband")
@Person(role="father")
@Person(role="son")
public ? class Man {
?? ?String name="";
}

在主方法中訪問該注解。

? ? public static void main(String[] args) {
? ? ? ? Annotation[] annotations = Man.class.getAnnotations(); ?
? ? ? ? System.out.println(annotations.length);
? ? ? ? Persons p1=(Persons) annotations[0];
? ? ? ? for(Person t:p1.value()){
? ? ? ? ?? ?System.out.println(t.role());
? ? ? ? }
? ? }

下面的代碼結(jié)果輸出相同,但是可以先判斷是否是相應(yīng)的注解,比較嚴(yán)謹(jǐn)。 

if(Man.class.isAnnotationPresent(Persons.class)) {
? ? Persons p2=Man.class.getAnnotation(Persons.class);
? ? for(Person t:p2.value()){
? ? ? ? System.out.println(t.role());
? ? }
?}

運(yùn)行結(jié)果:

1
CEO
husband
father
son

對@Repeatable的理解

@Repeatable是jdk8中新增的注解,使用如Spring中的@ComponentScan注解。

在沒有@Repeatable注解的的注解中,在同一個地方使用相同的注解會報錯,有了此元注解注解的注解,就可以在同一個地方使用相同的注解。

其官方文檔如下:

The annotation type {@code java.lang.annotation.Repeatable} is used to indicate that the annotation type whose declaration it (meta-)annotates is repeatable.

The value of @Repeatable indicates the containing annotation type for the repeatable annotation type.

@Repeatable 注解是用于聲明其它類型注解的元注解,來表示這個聲明的注解是可重復(fù)的。

@Repeatable的值是另一個注解,其可以通過這個另一個注解的值來包含這個可重復(fù)的注解。

示例

Value注解:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Values.class)
public @interface Value {
    String value() default "value";
}

Values注解:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Values {
    Value[] value();
}

其中,@Value注解上的元注解@Repeatable中的值,使用了@Values注解,@Values注解中包含的值類型是一個@Value注解的數(shù)組!

這就解釋了官方文檔中@Repeatable中值的使用。

測試

注解使用方法

public class AnnotationClass {
    @Value("hello")
    @Value("world")
    public void test(String var1, String var2) {
        System.out.println(var1 + " " + var2);
    }
}

測試用例

// 獲取使用`@Value`注解的`test`方法,并打印這個方法上的注解長度和信息
    @Test
    public void testValue() {
        Method[] methods = AnnotationClass.class.getMethods();
        for (Method method : methods){
            if (method.getName().equals("test")) {
                Annotation[] annotations = method.getDeclaredAnnotations();
                System.out.println(annotations.length);
                System.out.println(method.getName() + " = " + Arrays.toString(annotations));
            }
        }
    }

因?yàn)?code>test方法上使用了兩個@Value注解,所以猜測打印注解長度為2,然后打印詳情,可是結(jié)果并不同。

1
test = [@com.example.annotations.Values(value=[@com.example.annotations.Value(value=hello), @com.example.annotations.Value(value=world)])]

結(jié)果顯示,test方法上的注解長度為 1 , 且打印信息為@Values注解,它的值包含了使用的兩個注解。

因此可知在jdk8中,相同注解只是以集合的方式進(jìn)行了保存,原理并沒有變化。

注意事項(xiàng)

一些約束

@Repeatable 所聲明的注解,其元注解@Target的使用范圍要比@Repeatable的值聲明的注解中的@Target的范圍要大或相同,否則編譯器錯誤,顯示@Repeatable值所聲明的注解的元注解@Target不是@Repeatable聲明的注解的@Target的子集

// Value
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Values.class)
public @interface Value {
    String value() default "value";
}
// Values
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Values {
    Value[] value();
}

錯誤提示信息:

編譯錯誤提示信息

@Repeatable注解聲明的注解的元注解@Retention的周期要比@Repeatable的值指向的注解的@Retention得周期要小或相同。

周期長度為 SOURCE(源碼) < CLASS (字節(jié)碼) < RUNTIME(運(yùn)行)

// Value 注意 @Retention的值
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Values.class)
public @interface Value {
    String value() default "value";
}
// Values
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.CLASS)
public @interface Values {
    Value[] value();
}

編譯錯誤信息

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

繁峙县| 江孜县| 云梦县| 信阳市| 宁国市| 肇源县| 泾源县| 高尔夫| 龙泉市| 涿鹿县| 余干县| 开化县| 兴义市| 怀来县| 共和县| 罗源县| 邻水| 邵阳县| 西乌珠穆沁旗| 永顺县| 绥阳县| 南宫市| 双鸭山市| 宁强县| 福海县| 双辽市| 太仓市| 徐水县| 邵东县| 巩义市| 都昌县| 上栗县| 平罗县| 连州市| 临湘市| 桐城市| 沽源县| 商丘市| 平顺县| 乐都县| 阳信县|