2012-03-09 13 views
11

Sto tentando di abilitare il caching degli oggetti in un'applicazione Spring 3.1.1 esistente con Hibernate 3.5.5. Sto usando ehcache 2.2.0. Nel mio applicationContext ho aggiunto la configurazione per attivare la memorizzazione nella cache con EHCache.Configurazione di EHCache per Spring3.1.1 e Hibernate

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

Poi ho creato il file 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" /> 

ho aggiunto le dipendenze necessarie nel file pom.xml per EHCache. Ma ora sto ricevendo questo errore:

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

Qualcuno ha qualche idea di ciò che sta causando questo?

+1

Distinti dovrebbe funzionare ... Ecco la mia configurazione di lavoro se aiuta: aweigold

+0

@aweigold Grazie. Sembra funzionare per qualche motivo. Mi mancava l'elemento di proprietà. Perché non aggiungi il tuo commento come risposta in modo che io possa accettarlo. –

+0

Cool, felice che mi abbia aiutato. – aweigold

risposta

13

È necessario fare riferimento alla proprietà cacheManager in modo diverso. Questo è come l'ho lavorato:

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

Grazie! Questo ha funzionato per me, curioso del perché la configurazione documentata non ... –

11

risposta s' @aweigold è perfetto, ma una soluzione più chiara può essere raggiunto se si passa il riferimento di fagiolo 'EHCache' utilizzando 'p: cacheManager-ref'.

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

Lo stesso che in ultimo post solo senza errore nel nome dell'attributo:

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

Grazie a @Bogdan, ho corretto l'errore di battitura. – emrahkocaman

2

Includere la dipendenza sotto

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