我想设置 spring 方法缓存和由 ehcache 备份的休眠 2 级缓存:
在 spring-context.xml 中已经配置了 hibernate 2 级缓存:
<!-- hibernate level 2 cache configuration -->
<bean id="hibernateCacheManager"
class="net.sf.ehcache.CacheManager"
factory-bean="hibernateCacheManagerFactory"
factory-method="createCacheManager"
destroy-method="shutdown" />
<bean id="hibernateCacheManagerFactory"
class="com.project.CustomEhCacheManagerFactoryBean" />
缓存配置是从自定义 xml 文件中读取的,以便我们可以自定义需求并轻松测试缓存的实体:
public class CustomEhCacheManagerFactoryBean {
public CacheManager createCacheManager() {
final Configuration configuration = new Configuration();
// read the xml configuration
//for each node in xml
final CacheConfiguration cache = new CacheConfiguration();
// set the properties
configuration.addCache(cache);
// end for
cacheManager = CacheManager.create(configuration);
}
}
现在我想这次使用注释添加另一个缓存管理器的spring方法缓存。我在 spring-context.xml 中添加了以下内容:
<!-- suport method caching through annotation -->
<cache:annotation-driven />
<!-- spring method cache configuration -->
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="springEhcache"/>
</bean>
<bean id="springEhcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="cacheManagerName" value="method-cache" />
</bean>
然后我得到以下异常:
Caused by:
java.lang.IllegalArgumentException: loadCaches must not return an empty Collection
at org.springframework.util.Assert.notEmpty(Assert.java:268)
at org.springframework.cache.support.AbstractCacheManager.afterPropertiesSet(AbstractCacheManager.java:49)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
Q1
:不应该通过注释自动配置缓存吗?我错过了什么?
Q2
: 是否可以给这个bean idspringCacheManager
而不是cacheManager
?目前我得到异常:没有找到bean'cacheManager'
谢谢