【发布时间】: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