2012-08-07 42 views
0

我正在開發一個模塊,我正在計劃使用Spring's declarative caching來處理。我寫了一些方法,使用我可以使用Spring的緩存功能嗎?

@Override 
@Cacheable("businessUnitCache") 
public BusinessUnit getBusinessUnit(String businessUnitId){ 

我正打算提供一個classpath豆文件和classpath eh-cache configuration,而無需費時的項目就知道我執行的內部提供的功能和方法需要被緩存緩存(許多這些方法,他們永遠不會訪問直接)。

但是,閱讀Using Spring cache annotation in multiple modules的問題及其答案顯然會導致一個問題,即任何消耗項目都會使用Spring緩存註釋。我希望,如果沒有聲明緩存相匹配的註釋斯普林特將靜默失敗,但失敗,出現錯誤:

java.lang.IllegalArgumentException: Cannot find cache named [businessUnitCache] for CacheableOperation[public

導致我的結論,我不能使用高速緩存的註釋(與我衝突從問題Is it possible to use multiple ehcache.xml (in different projects, same war)?原來的結論我測試備份這件事

所以:。是否有可能分別從實現類聲明的緩存,最好是XML這將讓我有緩存準備額外的文件?規則,並使用標準的spring屬性替換替換緩存管理器名稱(我已經在做某些事情了與數據源類似)?不幸的是,refernece documentation只描述了基於註釋的配置。

回答

3

您可以使用XML文件配置緩存,見春參考手冊:

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/cache.html#cache-declarative-xml

<!-- the service we want to make cacheable --> 
<bean id="bookService" class="x.y.service.DefaultBookService"/> 

<!-- cache definitions --> 
<cache:advice id="cacheAdvice" cache-manager="cacheManager"> 
<cache:caching cache="books"> 
    <cache:cacheable method="findBook" key="#isbn"/> 
    <cache:cache-evict method="loadBooks" all-entries="true"/> 
</cache:caching> 
</cache:advice> 

<!-- apply the cacheable behaviour to all BookService interfaces --> 
<aop:config> 
<aop:advisor advice-ref="cacheAdvice" pointcut="execution(* x.y.BookService.*(..))"/> 
</aop:config> 
+0

它有助於看看'/電流/'DOC的版本,但遺憾的是谷歌通常不會將您鏈接到那裏。 – 2012-08-08 12:09:17

相關問題