为Spring3.1.1和Hibernate配置EHCache

Vin*_*nie 11 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" />
Run Code Online (Sandbox Code Playgroud)

然后我创建了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" /> 
Run Code Online (Sandbox Code Playgroud)

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

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

有谁知道是什么原因引起的?

awe*_*old 13

您需要以不同方式引用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"/>
Run Code Online (Sandbox Code Playgroud)


emr*_*man 11

@aweigold的答案是完美的,但是如果使用"p:cacheManager-ref"传递"ehcache"bean的引用,则可以实现更清晰的解决方案.

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
    p:cacheManager-ref="ehcache" />
Run Code Online (Sandbox Code Playgroud)