用于spring方法缓存和hibernate二级缓存的ehcache

时间:2012-12-24 10:50:03

标签: java spring hibernate ehcache

我想设置spring方法缓存和ehcache备份的hibernate二级缓存:

已在spring-context.xml中配置了hibernate二级缓存:

<!-- 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 ID springCacheManager代替cacheManager?目前我得到了异常:没有发现bean'cacheManager'

由于

0 个答案:

没有答案