@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);
以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。