声明式基于 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 方法用于将数据放入缓存,loadBooks 方法用于逐出数据。这两个定义都作用于 books 缓存。

aop:config 定义通过使用 AspectJ 切入点表达式将缓存建议应用于程序中的适当点(更多信息可在Spring 中的面向切面编程中找到)。在上述示例中,BookService 中的所有方法都被考虑,并对其应用缓存建议。

声明式 XML 缓存支持所有基于注解的模型,因此在这两者之间切换应该相当容易。此外,两者可以在同一个应用程序中使用。基于 XML 的方法不触及目标代码。然而,它本质上更加冗长。当处理具有重载方法并需要缓存的类时,识别正确的方法需要额外的努力,因为 method 参数不是一个好的判别器。在这些情况下,您可以使用 AspectJ 切入点来精确选择目标方法并应用适当的缓存功能。然而,通过 XML,更容易应用包或组或接口范围的缓存(同样,由于 AspectJ 切入点),并创建类似模板的定义(正如我们在前面的示例中通过 cache:definitionscache 属性定义目标缓存所做的那样)。

© . This site is unofficial and not affiliated with VMware.