32.5 缓存声明式 XML 配置

如果不想使用注解,可以使用 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 behavior to all BookService interfaces -->
<aop:config>
    <aop:advisor advice-ref="cacheAdvice" pointcut="execution(* x.y.BookService.*(..))"/>
</aop:config>

<!-- cache manager definition omitted -->

上面的配置中,bookService 是可配缓存的服务。在 cache:advice 指定方法 findBooks