经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
Spring?@Cacheable注解类内部调用失效的解决方案
来源:jb51  时间:2022/1/3 12:31:30  对本文有异议

@Cacheable注解类内部调用失效

如果你只是想使用一个轻量级的缓存方案,那么可以尝试使用Spring cache方案。

那么在使用spring @Cacheable注解的时候,要注意,如果类A的方法f()被标注了@Cacheable注解,那么当类A的其他方法,例如:f2(),去直接调用f()的时候,@Cacheable是不起作用的,原因是@Cacheable是基于spring aop代理类,f2()属于内部方法,直接调用f()时,是不走代理的。

举个例子:

  1. @Cacheable(key = "#entityType", value = "xxxCache")
  2. ? ? public List<String selectByEntityType(intentityType) {
  3. ? ? ? ? List<String> result = new ArrayList<>();
  4. ? ? ? ? //do something
  5. ? ? ? ? return result;
  6. ? ? }
  7. public List<String> f2(){
  8. ? //Cacheable失效,不会走缓存的
  9. ? selectByEntityType(1);
  10. }

可以把selectByEntityType方法抽取到另外的类中,例如:

  1. @Service
  2. public class CacheService{
  3. @Cacheable(key = "#entityType", value = "xxxCache")
  4. ? ? public List<String selectByEntityType(intentityType) {
  5. ? ? ? ? List<String> result = new ArrayList<>();
  6. ? ? ? ? //do something
  7. ? ? ? ? return result;
  8. ? ? }
  9. }

这样其他类要使用selectByEntityType方法,只能注入CacheService,走代理。

@Cacheable注解缓存方法内部调用

因为Spring Cache是基于切面的(基于AOP的动态代理实现的:即都在方法调用前后去获取方法的名称、参数、返回值,然后根据方法名称、参数生成缓存的key(自定义的key例外),进行缓存),所以内部方法调用不会调用切面,导致缓存不生效

方法一

暴露Aop代理到ThreadLocal支持,在类之前加@EnableAspectJAutoProxy(exposeProxy = true)

调用的时候使用((XxxService) AopContext.currentProxy()).method()调用方法

eg:

  1. ApiBaseResponse<ApiPageResponse<RoadCongestIndexData>> apiPageResponseApiBaseResponse =
  2. ? ? ? ? ? ? ? ? ((RoadLastPageServiceImpl) AopContext.currentProxy()).queryLastPageCongestIndexData1(request);

方法二

把需要用缓存的方法单独写到一个类里面,把内部调用变成类间调用

  1. RoadLastPageServiceImpl selfService = SpringContextUtil.getBean(RoadLastPageServiceImpl.class);
  2. ? ? ? ? selfService.queryLastPageCongestIndexData1(request);

方法三

类自我注入,使用@lazy和@Autowired注解实现自我注入,然后使用时用注解的实例代替this调用方法。

  1. @Lazy
  2. @Autowired
  3. private RoadLastPageServiceImpl serviceImplCache;

方法四

写一个工具类,使用内部调用的时候,自己实例化一个对象,让类走AOP

  1. @Component
  2. public class SpringContextUtil implements ApplicationContextAware {
  3. ? ? private static ApplicationContext applicationContext;
  4. ? ? /**
  5. ? ? ?* 实现ApplicationContextAware接口的回调方法,设置上下文环境
  6. ? ? ?*
  7. ? ? ?* @param applicationContext
  8. ? ? ?*/
  9. ? ? @Override
  10. ? ? public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  11. ? ? ? ? SpringContextUtil.applicationContext = applicationContext;
  12. ? ? }
  13. ? ? public static ApplicationContext getApplicationContext() {
  14. ? ? ? ? return applicationContext;
  15. ? ? }
  16. ? ? /**
  17. ? ? ?* 获取对象
  18. ? ? ?*
  19. ? ? ?* @param name
  20. ? ? ?* @return Object
  21. ? ? ?* @throws BeansException
  22. ? ? ?*/
  23. ? ? public static Object getBean(String name) throws BeansException {
  24. ? ? ? ? return applicationContext.getBean(name);
  25. ? ? }
  26. ? ? /**
  27. ? ? ?* 通过类型获取对象
  28. ? ? ?*
  29. ? ? ?* @param t
  30. ? ? ?* ? ? ? ? ? ?对象类型
  31. ? ? ?* @return
  32. ? ? ?* @throws BeansException
  33. ? ? ?*/
  34. ? ? public static <T> T getBean(Class<T> t) throws BeansException {
  35. ? ? ? ? return applicationContext.getBean(t);
  36. ? ? }
  37. }

调用的时候这么调用

  1. RoadLastPageServiceImpl selfService = SpringContextUtil.getBean(RoadLastPageServiceImpl.class);
  2. selfService.queryLastPageCongestIndexData1(request);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号