Spring Cache @Cacheable - not working while calling from another method of the same bean

后端 未结 9 1428
执念已碎
执念已碎 2020-11-27 10:06

Spring cache is not working when calling cached method from another method of the same bean.

Here is an example to explain my problem in clear way.<

相关标签:
9条回答
  • 2020-11-27 11:04

    In my Case I add variable :

    @Autowired
    private AService  aService;
    

    So I call the getEmployeeData method by using the aService

    @Named("aService")
    public class AService {
    
    @Cacheable("employeeData")
    public List<EmployeeData> getEmployeeData(Date date){
    ..println("Cache is not being used");
    ...
    }
    
    public List<EmployeeEnrichedData> getEmployeeEnrichedData(Date date){
        List<EmployeeData> employeeData = aService.getEmployeeData(date);
        ...
    }
    

    }

    It will use the cache in this case.

    0 讨论(0)
  • 2020-11-27 11:05

    If you call a cached method from same bean it will be treated as a private method and annotations will be ignored

    0 讨论(0)
  • 2020-11-27 11:08

    Use static weaving to create proxy around your bean. In this case even 'internal' methods would work correctly

    0 讨论(0)
提交回复
热议问题