2012-08-07 59 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

相关问题