代码之家  ›  专栏  ›  技术社区  ›  Peter Bernier

java5/6中的注解

  •  2
  • Peter Bernier  · 技术社区  · 6 年前

    我正在设置Hibernate缓存,并希望在不同区域缓存某些实体。例如,有些实体可以“过时”长达5分钟,有些可能会持续一小时,而有些实体(如查找)在数周内不会更改。为了便于在代码中方便地配置区域,我尝试以下操作:

    我创建了一个名为 @LookupCache (和 @DailyCache

    @Cache(region = "lookups", usage = CacheConcurrencyStrategy.READ_ONLY)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface LookupCache {}
    

    我将这个注释添加到Hibernate/JPA实体中:

    @LookupCache
    public class Course {}
    

    不必更改每个类的注释参数。

    但是,缓存加载器不会继承这个 @Cache 符号。我怎样才能得到

    更新:为了澄清,@Cache注释是一个内置的hibernate注释,供EHCache等二级缓存使用。我不能修改@Cache注释使其可由其他注释继承(我的客户机不想维护hibernate的特殊分支)。不过,这可能是我唯一的选择。

    1 回复  |  直到 15 年前
        1
  •  2
  •   Eugene Kuleshov    15 年前

    下面是一个简单的示例,演示如何检索应用于@LookupCache注释的@Cache注释的详细信息:

    Course c = new Course();
    LookupCache lookupCache = c.getClass().getAnnotation(LookupCache.class);
    Cache cache = lookupCache.annotationType().getAnnotation(Cache.class);
    System.err.println("region " + cache.region());
    System.err.println("usage " + cache.usage());
    

    您只需要确保@Cache注释也有@Retention(保留策略.RUNTIME)