簡體   English   中英

我可以在沒有注釋的情況下使用Spring的緩存功能嗎?

[英]Can I use Spring's caching features without the annotations?

我正在開發一個計划使用Spring的聲明式緩存來處理的模塊。 我用緩存寫了很多方法

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

我打算提供一個類路徑bean文件和類路徑eh-cache配置,以提供功能,而無需使用項目就知道我的實現的內部以及需要緩存的方法(其中許多方法永遠不會直接訪問)。

但是,閱讀在多個模塊中使用Spring緩存注釋的問題及其答案顯然會引起問題,因為任何正在使用的項目也都使用Spring緩存注釋。 我希望如果沒有與注釋匹配的已聲明緩存,Sprint會靜默失敗,但是失敗並顯示以下錯誤:

java.lang.IllegalArgumentException:找不到名為CacheableOperation [public]的名為[businessUnitCache]的緩存

導致我得出的結論是我不能使用緩存注釋(這與我的原始結論沖突: 是否可以使用多個ehcache.xml(在不同項目中,相同的戰爭)?我的測試對此進行了支持。

因此: 是否可以與實現類分開聲明緩存,最好在xml中聲明? 這將使我能夠准備具有緩存規則的其他文件,並使用標准的spring屬性替換來替換緩存管理器名稱(我已經對數據源進行了類似的操作)? 不幸的是, 參考文檔僅描述了基於注釋的配置。

您可以使用xml文件配置緩存,請參見spring參考手冊:

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM