重复注解

简介: 重复注解

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

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();
}

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

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

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();
    }
}

这样就可以重复注解了

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

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

Field field = UserInfo.class.getDeclaredField("serialVersionUID");
    BeanFieldSort empty = field.getAnnotation(BeanFieldSort.class);
        System.out.println("---");
        System.out.println(Objects.isNull(empty));
//        empty.order();        NPE

正确方式是使用

Field field = UserInfo.class.getDeclaredField("serialVersionUID");
BeanFieldSort.BeanFieldSorts annotation = field.getAnnotation(BeanFieldSort.BeanFieldSorts.class);
for (BeanFieldSort beanFieldSort : annotation.value()) {
    System.out.println(beanFieldSort.order());
}

即可

相关文章
|
Java 微服务 Spring
@EnableDiscoveryClient注解的作用
@EnableDiscoveryClient注解的作用 @EnableDiscoveryClient 及@EnableEurekaClient 类似,都是将一个微服务注册到Eureka Server(或其他 服务发现组件,例如Zookeeper、Consul等)
|
SQL Java 数据库连接
mybatis @Select注解中当参数为空则不添加该参数的判断
public interface OrderMapper extends SqlMapper{ @Select("select * from tbl_order where room like #{room} and mydate like #{my...
4449 0
|
10月前
|
Java
java注解(作用于注解)
java注解(作用于注解)
85 0
|
10月前
|
JSON 数据格式
@JsonProperty与@JSONField注解用处
@JsonProperty与@JSONField注解用处
|
11月前
|
Java
SpringBoot 自定义注解 + AOP实现参数效验,默认值赋值
SpringBoot 自定义注解 + AOP实现参数效验,默认值赋值
307 2
|
消息中间件 JavaScript 小程序
求求你们了,别再重复造轮子了,一个 Spring 注解轻松搞定循环重试功能!
求求你们了,别再重复造轮子了,一个 Spring 注解轻松搞定循环重试功能!
|
Java Spring 容器
添加@EnableAsync注解后报循环依赖,注入失败咋办
在PayService类中注入了payNotifyService的实例,而在PayNotifyService类中又注入了payService的实例。而PayNotifyService类中又有一个加了@Async 注解的方法A。
196 0
|
Java 数据库连接 Spring
SpringBoot启动类的扫描注解的用法及冲突原则
SpringBoot启动类的扫描注解的用法及冲突原则
415 0
SpringBoot启动类的扫描注解的用法及冲突原则
|
Java Spring 容器
《SpringBoot系列十二》:如何自定义条件装配(由@ConditionalOnClass推导)
《SpringBoot系列十二》:如何自定义条件装配(由@ConditionalOnClass推导)
284 0
《SpringBoot系列十二》:如何自定义条件装配(由@ConditionalOnClass推导)
|
XML 缓存 Java
Spring 源码阅读 51:查找注解配置的切面增强逻辑(1)- 查找配置类
本文分析了用于创建 AOP 代理的后处理器是如何查找基于注解配置的切面信息的,这是前半部分,主要包含切面配置类的查找和解析。
139 0
Spring 源码阅读 51:查找注解配置的切面增强逻辑(1)- 查找配置类