【问题标题】:Ehcache and spring bootstrapcacheloader not workingEhcache 和 spring bootstrapcacheloader 不工作
【发布时间】:2014-01-19 02:41:21
【问题描述】:

我正在尝试设置一个引导缓存加载程序,它将查询数据库并填充缓存。这里我使用的是与spring集成的ehcache。但问题是我无法将依赖项连接到我的缓存加载器实现中。 @Autowired、@Resource、@Configurable 它们似乎都不起作用。很明显,缓存加载器实例化不是由 Spring 容器完成的,但是有没有办法可以将 Spring 创建的缓存加载器实例注入缓存管理器并引导它? 我的实现细节如下。 ehcache.xml

<cache name="MyCache"
       maxElementsInMemory="100000"
       eternal="false" 
       overflowToDisk="false"
       timeToLiveSeconds="500">
      <!--  <pinning store="localMemory"/> -->
      <bootstrapCacheLoaderFactory class="net.tristargroup.claims.helper.ClaimsCacheLoaderFactory" properties="bootstrapAsynchronously=true"/>
       <cacheEventListenerFactory class="net.tristargroup.claims.helper.TristarCacheEventListenerFactory" listenFor="all"/>

</cache>

Spring 上下文 xml

<cache:annotation-driven/>

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager"><ref local="ehcache"/></property>
</bean>

<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" depends-on="cacheLoader">
    <property name="configLocation" value="classpath:ehcache.xml"/>
</bean>

缓存加载器类

 @Configurable

公共类 ClaimsCacheLoaderFactory 扩展 BootstrapCacheLoaderFactory{

@Resource
CacheManager cacheManager;
    .
    .
    .
    @Override
public BootstrapCacheLoader createBootstrapCacheLoader(Properties arg0) {
    System.out.println("Create cache loader method . Cache manager is ->"+cacheManager);
    BootstrapCacheLoader cacheLoader = new ClaimsCacheLoader();
    return cacheLoader;
}

即使我将它指定为 Autowired 属性,这里的 cacheManager 实例也始终为空。 即使在缓存事件侦听器中也存在问题。 有人请帮我解决这个问题。

【问题讨论】:

标签: spring caching ehcache terracotta


【解决方案1】:

如果 cacheManager 为空,这意味着您尚未使 bean 可用于您的包。在定义 bean 的同一位置添加 @ComponentScan("yourpackage") 并在要使用 cacheManager 的位置添加 @Component。 这是旧的,但我希望它可以帮助别人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 2017-09-06
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多