java - 为 Spring3.1.1 和 Hibernate 配置 EHCache

标签 java spring hibernate ehcache

我正在尝试使用 Hibernate 3.5.5 在现有 Spring 3.1.1 应用程序中启用对象缓存。我正在使用 ehcache 2.2.0。在我的 applicationContext 中,我添加了使用 EHCache 开启缓存的配置。

<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
    p:cache-manager="ehcache" />
<bean id="ehcache"
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
    p:config-location="ehcache.xml" />

然后我创建了 ehcache.xml 文件:

<diskStore path="java.io.tmpdir" />

<defaultCache 
    eternal="false" 
    maxElementsInMemory="1000"
    overflowToDisk="false" 
    diskPersistent="false" 
    timeToIdleSeconds="0"
    timeToLiveSeconds="0" 
    memoryStoreEvictionPolicy="LRU"/>

<cache name="studentCache" 
    eternal="false"
    maxElementsInMemory="10000" 
    overflowToDisk="false" 
    diskPersistent="false"
    timeToIdleSeconds="0" 
    timeToLiveSeconds="0"
    memoryStoreEvictionPolicy="LRU" /> 

我在 pom.xml 文件中为 ehcache 添加了必要的依赖项。但是现在我收到了这个错误:

Initialization of bean failed; 
nested exception is org.springframework.beans.ConversionNotSupportedException: 
Failed to convert property value of type 'java.lang.String' to required type
'net.sf.ehcache.CacheManager' for property 'cacheManager'; 
nested exception is java.lang.IllegalStateException: 
Cannot convert value of type [java.lang.String] to required type
[net.sf.ehcache.CacheManager] for property 'cacheManager': 
no matching editors or conversion strategy found

有人知道是什么原因造成的吗?

最佳答案

您需要以不同的方式引用您的 cacheManager 属性。这就是我的工作方式:

<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" p:configLocation="classpath:ehcache.xml"/>

关于java - 为 Spring3.1.1 和 Hibernate 配置 EHCache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9636793/

相关文章:

java - 引入参数对象后Jasper不渲染

java - JPA 是否要求数据源来自 JNDI?

java - 仅在子类上为列添加唯一约束

java.sql.BatchUpdateException : ORA-00001: unique constraint while we generating primary key

java - 带注解的 Hibernate 拦截器

java - 未找到 Spring-Social 依赖关系

java - 下面两组代码有什么区别

java - 无法让@Autowired 与 bean 一起工作

java - Spring MVC - 绑定(bind)日期字段

java - 使用 CommonsMultipartResolver 上传多个文件时如何为每个文件设置 maxUploadSize?