问题
在springboot中使用Cacheable,通过构造器@RequiredArgsConstructor注入实例,在Controller中调用service的getOne方法,getOne方法上使用@Cacheable无效。
原因
我这里是循环引用的问题,在Controller 里面我注入了service的实例,但同时我还注入了另外一个组件,在组件里面也需要用到该service,所以在组件里面也注入了该service。层结构大概这个样子:
controller
--tService
--tComponent
--tService
解决
在组件上使用@RequiredArgsConstructor(onConstructor_= { @Autowired, @Lazy }),顺利解决循坏依赖的问题。