场景
使用 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