无法使用弹簧@Cacheable和@EnableCaching
2022-09-04 01:31:15
我正在尝试替换我的旧:
@Component
public interface MyEntityRepository extends JpaRepository<MyEntity, Integer> {
@QueryHints({@QueryHint(name = CACHEABLE, value = "true")})
MyEntity findByName(String name);
}
通过这个:
@Component
public interface MyEntityRepository extends JpaRepository<MyEntity, Integer> {
@Cacheable(value = "entities")
MyEntity findByName(String name);
}
因为我想使用高级缓存功能,例如不缓存空值等。
为此,我按照春季教程 https://spring.io/guides/gs/caching/
如果我不注释我的应用程序.java,缓存根本不起作用。
但是,如果我添加一个 CacheManager bean:@EnableCaching
package my.application.config;
@EnableWebMvc
@ComponentScan(basePackages = {"my.application"})
@Configuration
@EnableCaching
public class Application extends WebMvcConfigurerAdapter {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("entities");
}
// ...
}
我在启动时收到以下错误:
java.lang.IllegalStateException:未指定 CacheResolver,也未找到 CacheManager 类型的 Bean。注册 CacheManager Bean 或从配置中删除@EnableCaching注释
如果我用 CacheResolver Bean 替换 My CacheManager Bean,我得到同样的错误,如下所示:
@Bean
public CacheResolver cacheResolver() {
return new SimpleCacheResolver(new ConcurrentMapCacheManager("entities"));
}
我错过了什么吗?