温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring @Cacheable注解类内部调用失效的解决方法

发布时间:2022-01-04 00:30:07 来源:亿速云 阅读:377 作者:柒染 栏目:开发技术

这期内容当中小编将会给大家带来有关Spring @Cacheable注解类内部调用失效的解决方法,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

@Cacheable注解类内部调用失效

如果你只是想使用一个轻量级的缓存方案,那么可以尝试使用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方法抽取到另外的类中,例如:

@Service
public class CacheService{
@Cacheable(key = "#entityType", value = "xxxCache")
    public List<String selectByEntityType(intentityType) {
        List<String> result = new ArrayList<>();
        //do something
        return result;
    }
}

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

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

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

方法一

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

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

eg:

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

方法二

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

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

方法三

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

@Lazy
@Autowired
private RoadLastPageServiceImpl serviceImplCache;

方法四

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

@Component
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    /**
     * 实现ApplicationContextAware接口的回调方法,设置上下文环境
     *
     * @param applicationContext
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    /**
     * 获取对象
     *
     * @param name
     * @return Object
     * @throws BeansException
     */
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }
    /**
     * 通过类型获取对象
     *
     * @param t
     *            对象类型
     * @return
     * @throws BeansException
     */
    public static <T> T getBean(Class<T> t) throws BeansException {
        return applicationContext.getBean(t);
    }
}

调用的时候这么调用

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

上述就是小编为大家分享的Spring @Cacheable注解类内部调用失效的解决方法了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI