为了实现业务层缓存,定义了几个注解:@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 "" } }
对一个业务方法来讲,前两个注解不必添加多个,但 缓存清除 的注解在有些时候须要屡次引用,好比:ide
@Override @Cache.del(key="#(id)") @Cache.del(key="ItemMap") public boolean deleteById(String id) { return super.deleteById(id); }
以上的业务方法很简单,就是根据ID删除指定的Modelui
但对于缓存来讲,不只要清除相应Key值的单条数据,还要清除包含这个Model的集合数据spa
因此就须要为这个业务方法添加两个 @Cache.del 注解来实现上面的需求code
但此时会发现IDE的错误提示:blog
Duplicate annotation of non-repeatable type @Cache.del.
Only annotation types marked @Repeatable can be used multiple times at one target.
个人环境是JDK8,支持重复注解就是JDK8的一个新特性,下面就来试验一下ip
按照提示,给 @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(); } //容器内定义指定类型的数组
代码中的红色部分是须要特殊注意的get
其中对于原有的 @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对于重复注解的实现其实就是个语法糖,内部实现仍是注解嵌套的模式
但在开发体验上确实比之前好多了!