我们知道缓存方法的调用是通过spring aop切入的调用的。在一个类调用另一个类中的方法可以直接的简单调用,但是如果在同一个类中调用自己已经通过spring托管的类中的方法该如何实现呢?
先来段代码:
public List<Long> getSkuIdsBySpuId(long spuId) { ItemComposite itemComposite = this.getItemComposite(spuId);///能走下面的缓存吗? if (itemComposite!=null) { if ( CollectionUtils.isNotEmpty(itemComposite.getItemSkus())) { return itemComposite.getItemSkus().stream().map(itemSku -> itemSku.getId()).collect(Collectors.toList()); } } return Collections.emptyList();}@Cacheable(value = "getItemComposite", key = "#spuId")public ItemComposite getItemComposite(long spuId) { //select from db...}public List<Long> getSkuIdsBySpuId(long spuId) { ItemCacheManager itemCacheManager = (ItemCacheManager)AopContext.currentProxy(); if (itemComposite!=null) { if ( CollectionUtils.isNotEmpty(itemComposite.getItemSkus())) { return itemComposite.getItemSkus().stream().map(itemSku -> itemSku.getId()).collect(Collectors.toList()); } } return Collections.emptyList();}@Cacheable(value = "getItemComposite", key = "#spuId")public ItemComposite getItemComposite(long spuId) { //select from db...}可以看到修改的地方是通过调用AopContext.currentProxy的方式去拿到代理类来调用getItemComposite方法。这样就结束了?不是,通过调试发现会抛出异常:java.lang.IllegalStateException: Cannot find current proxy: Set ‘exposeProxy‘ property on Advised to ‘true‘ to make it available.
继续查找资料,csdn上一篇文章正好有篇文章http://blog.csdn.net/z69183787/article/details/45622821是讲述这个问题的,他给的解决方法是在applicationContext.xml中添加一段<aop:aspectj-autoproxy proxy-target-class="true"expose-proxy="true"/>
。但是与我们的系统不同的是,我们系统是通过spring-boot来启动的,目前都是通过annotation来代替配置文件的,所以我们必须找到一个annotation来代替这段配置,发现在ApplicationMain中加入@EnableAspectJAutoProxy(proxyTargetClass=true)然后添加maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency>可以解决我们的问题,这时候你一定认为事情可以大功告成了,但是真正的坑来了:我们的spring-boot版本是1.3.5,版本过低,这种注解必须是高版本才能支持,有种想死的感觉。。。。怎么办?
还是想想csdn上的那篇文章,通过配置文件是可以解决的,那么我们就在spring boot中导入配置文件应该就没问题了啊。
于是我们可以配置一个aop.xml文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> xsi:schemaLocation="http://www.springframework.org/schema/beans <aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/></beans>@ImportResource(locations = "aop.xml")原文:http://www.cnblogs.com/fanguangdexiaoyuer/p/7620534.html