Spring @Cacheable注解类内部调用失效问题
来源:互联网 发布:果蔬清洗机有用吗 知乎 编辑:程序博客网 时间:2024/06/05 03:04
如果你只是想使用一个轻量级的缓存方案,那么可以尝试使用Spring cache方案。
但是,在使用Spring @Cacheable注解的时候,要注意,如果类A的方法f()被标注了@Cacheable注解,那么当类A的其他方法,例如:f2(),去直接调用f()的时候,@Cacheable是不起作用的,原因是@Cacheable是基于Spring AOP代理类,f2()属于内部方法,直接调用f()时,是不走代理的。举个例子:
@Cacheable(key = "#entityType", value = "xxxCache")public List<String selectByEntityType(intentityType) { List<String> result = new ArrayList<>(); // do something return result;}public List<String> f2(){ // Cacheable失效,不会走缓存的 selectByEntityType(1);}
可以把selectByEntityType方法抽取到另外的类中,例如:
@Servicepublic class CacheService { @Cacheable(key = "#entityType", value = "xxxCache") public List<String selectByEntityType(intentityType) { List<String> result = new ArrayList<>(); // do something return result; }}
这样其他类要使用selectByEntityType方法,只能注入CacheService,走代理。
转自:http://blog.csdn.net/linsongbin1/article/details/51304138
1 0
- Spring @Cacheable注解类内部调用失效问题
- Spring Cacheable标签 内部调用失效问题
- Spring Cacheable标签 内部调用失效问题
- Spring的Bean内部方法调用无法使用AOP切面(CacheAble注解失效)
- 关于spring 的@cacheable 注解 无法生效的问题
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring 缓存注解@Cacheable的用法
- Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用
- Spring 缓存注解@Cacheable的用法
- Spring 缓存注解(@Cacheable、@CachePut、@CacheEvict)使用
- Spring 缓存注解@Cacheable的用法
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CachePut和@CacheEvict
- Spring Cacheable注解不缓存null值
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring 缓存注解@Cacheable的用法
- Spring缓存注解@Cacheable,@CachePut , @CacheEvict介绍
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- C:\Ruby22\bin\ruby.exe: invalid option --no-cache (-h will show valid options) (RuntimeError)
- linux oracle11g客户端安装笔记
- 卷积神经网络(CNNs / ConvNets) 一
- 自定义滚动条(1)
- RecycleView添加分割线
- Spring @Cacheable注解类内部调用失效问题
- 微信支付
- 携程Android App插件化和动态加载实践
- Struts2---拦截器
- 放在WEB-INF下的jsp文件无法被直接读取
- JavaScript数组
- 自定义滚动条(2)
- how to debug c problem
- web前端表格学习