jdk8环境下,添加重复注解的美好体验

为了实现业务层缓存,定义了几个注解:@Cache.able@Cache.put@Cache.del

分别实现对业务方法的 缓存检测缓存插入 缓存清除

public @interface Cache {

    /**
     * 缓存检测
     * @author netwild
     */
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ ElementType.METHOD })
    public static @interface able{ String cache() default ""; String key() default ""; }


    /**
     * 缓存插入
     * @author netwild
     */
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ ElementType.METHOD })
    public static @interface put{ String cache() default ""; String key() default ""; }

    /**
     * 缓存清除
     * @author netwild
     */
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ ElementType.METHOD })
    public static @interface del{ String cache() default ""; String key() default "" }
    
}

对一个业务方法来说,前两个注解没必要添加多个,但 缓存清除 的注解在有些时候需要多次引用,比如:

@Override
@Cache.del(key="#(id)")
@Cache.del(key="ItemMap")
public boolean deleteById(String id) {
    return super.deleteById(id);
}

以上的业务方法很简单,就是根据ID删除指定的Model

但对于缓存来说,不仅要清除相应Key值的单条数据,还要清除包含这个Model的集合数据

所以就需要为这个业务方法添加两个 @Cache.del 注解来实现上面的需求

但此时会发现IDE的错误提示:

Duplicate annotation of non-repeatable type @Cache.del. 
Only annotation types marked @Repeatable can be used multiple times at one target.

我的环境是JDK8,支持重复注解就是JDK8的一个新特性,下面就来试验一下

按照提示,给 @Cache.del 增加 @Repeatable 子注解,同时创建一个包含 @Cache.del 的容器注解:

    /**
     * 缓存清除
     * @author netwild
     */
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ ElementType.METHOD })
    @Repeatable(Cache.dels.class)    //支持重复注解,同时指定容器注解
    public static @interface del{ String cache() default ""; String key() default "" }

    /**
     * 缓存清除容器 
     * @author netwild
     */
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ ElementType.METHOD })
    public static @interface dels{ Cache.del[] value(); }  //容器内定义指定类型的数组

代码中的红色部分是需要特殊注意的

其中对于原有的 @Cache.del 增加了 @Repeatable 子注解,说明该注解可以重复使用,同时指定了重复注解的 数据保存容器

之后增加的 @Cache.dels 就是容器注解,只在获取注解数据时使用,平时这个注解属于隐身状态

上面的修改完成之后,会发现之前在业务方法上添加的重复注解不再提示错误了,OK

最后看看如何获取重复注解的数据:

Annotation[] annos = method.getAnnotations();
if(annos.length > 0){
    Arrays.stream(annos).forEach(anno -> {
        if(anno instanceof Cache.del){ //单条清除注解
            Cache.del temp = (Cache.del)anno;
            String cacheName = buildCacheName(temp.cache());
            String cacheKey = buildCacheKey(temp.key());
            //具体处理逻辑
        }else if(anno instanceof Cache.dels){ //多条清除注解
            Cache.dels dels = (Cache.dels)anno;
            Cache.del[] delarr = dels.value();
            Arrays.stream(delarr).forEach(temp -> {
                String cacheName = temp.cache();
                String cacheKey = temp.key();
                //具体处理逻辑
            }
        }
    });
}

在遍历注解时,需要同时判断单条注解和重复注解两种情况

如果业务方法只添加了一个@Cache.del注解,那么不会执行注解容器;否则需要从容器中获得重复注解的数据

看到最后,我们才发现,JDK8对于重复注解的实现其实就是个语法糖,内部实现还是注解嵌套的模式

但在开发体验上确实比以前好多了!

原文地址:https://www.cnblogs.com/netWild/p/9327306.html