51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

重复注解

英雄非无泪,不洒敌人前。男儿七尺躯,愿为祖国捐。------陈辉

java中如果我们需要一个注解能被重复使用

例如这个

|------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | hljs java package com.ruben.annotation; import java.lang.annotation.*; /** * @ClassName: BeanFieldSort * @Description: * @Date: 2020/9/11 22:18 * * * @author: achao<achao1441470436 @ gmail.com> * @version: 1.0 * @since: JDK 1.8 */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface BeanFieldSort { /** * 序号 * * @return */ int order(); } |

如果我们直接重复注解,会发现编译错误

image-20210211134907218

我们需要在注解上加上@Repeatable注解,里面参数放另外一个注解,作为它的承载

|------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | hljs java package com.ruben.annotation; import java.lang.annotation.*; /** * @ClassName: BeanFieldSort * @Description: * @Date: 2020/9/11 22:18 * * * @author: achao<achao1441470436 @ gmail.com> * @version: 1.0 * @since: JDK 1.8 */ @Repeatable(BeanFieldSort.BeanFieldSorts.class) @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface BeanFieldSort { /** * 序号 * * @return */ int order(); @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @interface BeanFieldSorts { BeanFieldSort[] value(); } } |

这样就可以重复注解了

image-20210211135034374

如果我们需要取出注解里面的order

使用之前的方式就会报空指针了,运行结果也打印出来为true

|-------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 | hljs java Field field = UserInfo.class.getDeclaredField("serialVersionUID"); BeanFieldSort empty = field.getAnnotation(BeanFieldSort.class); System.out.println("---"); System.out.println(Objects.isNull(empty)); // empty.order(); NPE |

image-20210211135659010

正确方式是使用

|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 | hljs java Field field = UserInfo.class.getDeclaredField("serialVersionUID"); BeanFieldSort.BeanFieldSorts annotation = field.getAnnotation(BeanFieldSort.BeanFieldSorts.class); for (BeanFieldSort beanFieldSort : annotation.value()) { System.out.println(beanFieldSort.order()); } |

即可

image-20210211135736456

赞(3)
未经允许不得转载:工具盒子 » 重复注解