Configuración de EHCache para Spring3.1.1 e Hibernate

I am attempting to enable object caching in an existing Spring 3.1.1 application with Hibernate 3.5.5. I am using ehcache 2.2.0. In my applicationContext I have added the configuration to switch on caching with 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" />

I then created the ehcache.xml file:

<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" /> 

I added the necessary dependencies in the pom.xml file for ehcache. But now I am getting this error:

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

¿Alguien tiene alguna idea de qué está causando esto?

preguntado el 09 de marzo de 12 a las 15:03

Yours should work... here is my working configuration if it helps: <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"/> -

@aweigold Thanks. That seem to work for some reason. I was missing the property element. Why don't you add your comment as an answer so that I can accept it. -

4 Respuestas

You need to reference your cacheManager property differently. This is how I have it working:

<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"/>

respondido 09 mar '12, 19:03

@aweigold 's answer is perfect but a clearer solution can be achieved if you pass the reference of "ehcache" bean using "p:cacheManager-ref".

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
    p:cacheManager-ref="ehcache" />

Respondido 12 Abr '13, 21:04

Include the below dependency

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.3.1</version>
</dependency>

Respondido el 04 de Septiembre de 13 a las 21:09

The same as in last post only without mistake in attribute name:

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehcache" />

Respondido el 01 de enero de 13 a las 12:01

Thanks @Bogdan, I've fixed the typo. - emrahkocaman

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.