Подтвердить что ты не робот

Настройка EHCache для Spring3.1.1 и Hibernate

Я пытаюсь включить кэширование объектов в существующем приложении Spring 3.1.1 с Hibernate 3.5.5. Я использую ehcache 2.2.0. В моем приложенииContext я добавил конфигурацию для включения кеширования с 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

Кто-нибудь знает, что вызывает это?

4b9b3361

Ответ 1

Вам нужно ссылаться на свойство 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"/>

Ответ 2

Ответ на

@aweigold идеален, но более четкое решение может быть достигнуто, если вы передадите ссылку "ehcache" bean с помощью "p: cacheManager-ref".

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

Ответ 3

Включить следующую зависимость

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

Ответ 4

То же, что и в последнем сообщении, только без ошибок в имени атрибута:

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