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

Java注解之Repeatable解讀

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

Java注解之Repeatable

Repeatable使用場(chǎng)景

在需要對(duì)同一種注解多次使用時(shí),往往需要借助@Repeatable。

實(shí)例

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

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

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

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

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

Person注解:

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

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

聲明一個(gè)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

對(duì)@Repeatable的理解

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

在沒有@Repeatable注解的的注解中,在同一個(gè)地方使用相同的注解會(huì)報(bào)錯(cuò),有了此元注解注解的注解,就可以在同一個(gè)地方使用相同的注解。

其官方文檔如下:

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 注解是用于聲明其它類型注解的元注解,來表示這個(gè)聲明的注解是可重復(fù)的。

@Repeatable的值是另一個(gè)注解,其可以通過這個(gè)另一個(gè)注解的值來包含這個(gè)可重復(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注解中包含的值類型是一個(gè)@Value注解的數(shù)組!

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

測(cè)試

注解使用方法

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

測(cè)試用例

// 獲取使用`@Value`注解的`test`方法,并打印這個(gè)方法上的注解長(zhǎng)度和信息
    @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方法上使用了兩個(gè)@Value注解,所以猜測(cè)打印注解長(zhǎng)度為2,然后打印詳情,可是結(jié)果并不同。

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

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

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

注意事項(xiàng)

一些約束

@Repeatable 所聲明的注解,其元注解@Target的使用范圍要比@Repeatable的值聲明的注解中的@Target的范圍要大或相同,否則編譯器錯(cuò)誤,顯示@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();
}

錯(cuò)誤提示信息:

編譯錯(cuò)誤提示信息

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

周期長(zhǎng)度為 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();
}

編譯錯(cuò)誤信息

總結(jié)

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

相關(guān)文章

最新評(píng)論

韶山市| 军事| 东台市| 澄迈县| 宁德市| 东城区| 剑河县| 闽侯县| 台中市| 曲阜市| 筠连县| 横山县| 延边| 平果县| 德兴市| 宁城县| 无极县| 彰化县| 搜索| 武鸣县| 洛扎县| 五常市| 增城市| 临沧市| 新余市| 广平县| 沙坪坝区| 丽江市| 蕉岭县| 宁都县| 都匀市| 额济纳旗| 双柏县| 即墨市| 丰顺县| 平乡县| 讷河市| 云阳县| 裕民县| 大丰市| 厦门市|