我正在尝试使用 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/