2012-03-09 5 views
11

Tôi đang cố gắng kích hoạt bộ nhớ đệm đối tượng trong ứng dụng Spring 3.1.1 hiện có với Hibernate 3.5.5. Tôi đang sử dụng ehcache 2.2.0. Trong applicationContext của tôi, tôi đã thêm cấu hình để chuyển sang bộ nhớ đệm với EHCache.Cấu hình EHCache cho Spring3.1.1 và 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" /> 

Sau đó tôi tạo 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" /> 

tôi đã thêm các phụ thuộc cần thiết trong file pom.xml cho ehcache. Nhưng bây giờ tôi gặp phải lỗi này:

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

Có ai biết điều gì đang gây ra điều này không?

+1

Yours nên làm việc ... đây là cấu hình làm việc của tôi nếu nó giúp: aweigold

+0

@aweigold Cảm ơn. Điều đó dường như làm việc vì một lý do nào đó. Tôi đã thiếu yếu tố tài sản. Tại sao bạn không thêm nhận xét của mình làm câu trả lời để tôi có thể chấp nhận. –

+0

Tuyệt vời, vui vì điều đó đã giúp ích. – aweigold

Trả lời

13

Bạn cần tham chiếu thuộc tính cacheManager của mình khác nhau. Đây là cách tôi có nó làm việc:

<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

Cảm ơn! Điều này làm việc cho tôi, tò mò tại sao cấu hình tài liệu không ... –

11

câu trả lời @aweigold 's là hoàn hảo nhưng một giải pháp rõ ràng hơn có thể đạt được nếu bạn vượt qua các tài liệu tham khảo của 'ehcache' đậu sử dụng 'p: cacheManager-ref'.

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

Giống như trong bài viết cuối cùng chỉ không sai lầm trong tên thuộc tính:

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

Cảm ơn @Bogdan, tôi đã sửa lỗi đánh máy. – emrahkocaman

2

Bao gồm phụ thuộc dưới

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