SpringBoot使用缓存报错java.lang.IllegalStateException: No cache could be resolved for 'Builder[public com.example.bean.Employee
SpringBoot使用缓存报错java.lang.IllegalStateException: No cache could be resolved for 'Builder[public com.example.bean.Employee
报错如下:
java.lang.IllegalStateException: No cache could be resolved for 'Builder[public com.example.bean.Employee com.example.controller.EmployeeController.getEmpById(java.lang.Integer)] caches=[] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'' using resolver 'org.springframework.cache.interceptor.SimpleCacheResolver@540d3d5d'. At least one cache should be provided per cache operation.
解决方法:
1、在SpringBoot启动类上添加@EnableCaching
注解。
2、在controller中需要加入缓存的方法中添加@Cacheable
,需要指定cacheNames缓存组件的名称。
@Cacheable(cacheNames = "emp")
@GetMapping("/getEmpById/{id}")
public Employee getEmpById(@PathVariable("id") Integer id) {
return employeeService.getEmpById(id);
}
例如上面需要指定cacheNames的值,名字随意。