因为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 Cache基于AOP的缓存机制,指出内部方法调用会导致缓存失效的原因,并列举了四种解决策略:1.启用AopContext支持;2.将方法移到独立类中;3.通过@Lazy和@Autowired自我注入;4.利用工具类实例化对象以触发AOP代理。
652

被折叠的 条评论
为什么被折叠?



