Spring aop 内部调用、自调用不生效问题与解决方案

场景

使用 spring cache 框架时 服务类内部方法调用并不触发缓存动作

演示

1[[@Service](http://my.oschina.net/service)](http://my.oschina.net/service) 2public class CacheTestService { 3 4 5 @Cacheable(value = "test_cache", key = "'method'") 6 public String method() { 7 System.out.println("method"); 8 return method1(1) + "_" + 9 method2(2) + "_" + 10 method3(3) + "_" + 11 method4(4) 12 ; 13 } 14 15 @Cacheable(value = "test_cache", key = "'method'+#i") 16 public String method1(int i) { 17 System.out.println("method1"); 18 return "method1_" + i; 19 } 20 21 @Cacheable(value = "test_cache", key = "'method'+#i") 22 protected String method2(int i) { 23 System.out.println("method2"); 24 return "method2_" + i; 25 } 26 27 @Cacheable(value = "test_cache", key = "'method'+#i") 28 String method3(int i) { 29 System.out.println("method3"); 30 return "method3_" + i; 31 } 32 33 @Cacheable(value = "test_cache", key = "'method'+#i") 34 private String method4(int i) { 35 System.out.println("method4"); 36 return "method4_" + i; 37 } 38} 39

查看redis

1127.0.0.1:6379> keys cache:* 21) "cache://test_cache:method" 3

可以看到 method1、method2、method3、method4 方法的缓存并没有生效

原因:

注意和限制 基于 proxy 的 spring aop 带来的内部调用问题 上面介绍过 spring cache 的原理,即它是基于动态生成的 proxy 代理机制来对方法的调用进行切面,这里关键点是对象的引用问题,如果对象的方法是内部调用 (即 this 引用)而不是外部引用,则会导致 proxy 失效,那么我们的切面就失效,也就是说上面定义的各种注释包括 @Cacheable、@CachePut 和 @CacheEvict 都会失效

解决方案

1public interface BeanSelfAware { 2 void setSelf(Object proxyBean); 3} 4 5 6@Component 7public class InjectBeanSelfProcessor implements BeanPostProcessor, ApplicationContextAware { 8 private ApplicationContext context; 9 //① 注入ApplicationContext 10 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 11 this.context = applicationContext; 12 } 13 public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { 14 if(!(bean instanceof BeanSelfAware)) { //② 如果Bean没有实现BeanSelfAware标识接口 跳过 15 return bean; 16 } 17 if(AopUtils.isAopProxy(bean)) { //③ 如果当前对象是AOP代理对象,直接注入 18 ((BeanSelfAware) bean).setSelf(bean); 19 } else { 20 //④ 如果当前对象不是AOP代理,则通过context.getBean(beanName)获取代理对象并注入 21 //此种方式不适合解决prototype Bean的代理对象注入 22 ((BeanSelfAware)bean).setSelf(context.getBean(beanName)); 23 } 24 return bean; 25 } 26 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { 27 return bean; 28 } 29}

服务类:

1@Service 2public class CacheTestService implements BeanSelfAware { 3 4 5 @Cacheable(value = "test_cache", key = "'method'") 6 public String method() { 7 System.out.println("method"); 8 return proxySelf.method1(1) + "_" + 9 proxySelf.method2(2) + "_" + 10 proxySelf.method3(3) + "_" + 11 proxySelf.method4(4) 12 ; 13 } 14 15 @Cacheable(value = "test_cache", key = "'method'+#i") 16 public String method1(int i) { 17 System.out.println("method1"); 18 return "method1_" + i; 19 } 20 21 @Cacheable(value = "test_cache", key = "'method'+#i") 22 protected String method2(int i) { 23 System.out.println("method2"); 24 return "method2_" + i; 25 } 26 27 @Cacheable(value = "test_cache", key = "'method'+#i") 28 String method3(int i) { 29 System.out.println("method3"); 30 return "method3_" + i; 31 } 32 33 @Cacheable(value = "test_cache", key = "'method'+#i") 34 private String method4(int i) { 35 System.out.println("method4"); 36 return "method4_" + i; 37 } 38 39 40 CacheTestService proxySelf; 41 42 @Override 43 public void setSelf(Object proxyBean) { 44 this.proxySelf = (CacheTestService) proxyBean; 45 } 46} 47

再次查看 redis

1127.0.0.1:6379> keys cache:* 21) "cache://test_cache:method1" 32) "cache://test_cache:method" 4

方法 method1 缓存动作成功执行,但是以protected、默认、private修饰的方法签名并没有生效 因此 此方案还需注意使用public修饰被调用方法

...

骚年,你以为这样就完了? 不 还有坑!

启用 aop 时 请使用

1 <!-- 用这个 --> 2 <aop:aspectj-autoproxy proxy-target-class="true" /> 3 <!-- 不要用这个 --> 4 <!--<aop:config proxy-target-class="true" />-->

over

参考资料

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )