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

网友投稿 948 2022-09-05


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

目录@Cacheable注解类内部调用失效@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

List();

//do something

return result;

}

public List f2(){

//Cacheable失效,不会走缓存的

selectByEntityType(1);

}

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

@Service

public class CacheService{

@Cacheable(key = "#entityType", value = "xxxCache")

public List

List 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> 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 applicaELAKwtionContext;

}

/**

* 获取对象

*

* @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 getBean(Class t) throws BeansException {

return applicationContext.getBean(t);

}

}

调用的时候这么调用

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

selfService.queryLastPageCongestIndexData1(request);

List();

//do something

return result;

}

public List f2(){

//Cacheable失效,不会走缓存的

selectByEntityType(1);

}

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

@Service

public class CacheService{

@Cacheable(key = "#entityType", value = "xxxCache")

public List

List 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> 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 applicaELAKwtionContext;

}

/**

* 获取对象

*

* @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 getBean(Class t) throws BeansException {

return applicationContext.getBean(t);

}

}

调用的时候这么调用

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

selfService.queryLastPageCongestIndexData1(request);

List 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> 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 applicaELAKwtionContext;

}

/**

* 获取对象

*

* @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 getBean(Class t) throws BeansException {

return applicationContext.getBean(t);

}

}

调用的时候这么调用

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

selfService.queryLastPageCongestIndexData1(request);


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:用 VIM 搭建 Python 的集成开发环境(用一生去爱你)
下一篇:some python trap and hints(somebody that I used to know)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~