java 无法使用 Spring @Cacheable 和 @EnableCaching

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32324832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:00:59  来源:igfitidea点击:

Unable to use Spring @Cacheable and @EnableCaching

javaspringcachingspring-data

提问by Pleymor

I'm trying to replace my old:

我正在尝试更换旧的:

@Component
public interface MyEntityRepository extends JpaRepository<MyEntity, Integer> {

    @QueryHints({@QueryHint(name = CACHEABLE, value = "true")})
    MyEntity findByName(String name);
}

by this:

这样:

@Component
public interface MyEntityRepository extends JpaRepository<MyEntity, Integer> {

    @Cacheable(value = "entities")
    MyEntity findByName(String name);
}

Because I want to use advanced caching features like no caching of null values, etc.

因为我想使用高级缓存功能,例如不缓存空值等。

To do so, I followed Spring tutorial https://spring.io/guides/gs/caching/

为此,我遵循了 Spring 教程https://spring.io/guides/gs/caching/

If I don't annotate my Application.java, caching simply doesn't work.

如果我不注释我的 Application.java,缓存根本不起作用。

But if I add @EnableCachingand a CacheManager bean:

但是如果我添加@EnableCaching一个 CacheManager bean:

package my.application.config;

@EnableWebMvc
@ComponentScan(basePackages = {"my.application"})
@Configuration
@EnableCaching
public class Application extends WebMvcConfigurerAdapter {

    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("entities");
    }

// ...
}

I get the following error at startup:

我在启动时收到以下错误:

java.lang.IllegalStateException: No CacheResolver specified, and no bean of type CacheManager found. Register a CacheManager bean or remove the @EnableCaching annotation from your configuration

java.lang.IllegalStateException:未指定 CacheResolver,也未找到 CacheManager 类型的 bean。注册一个 CacheManager bean 或从您的配置中删除 @EnableCaching 注释

I get the same error if I replace My CacheManager bean by a CacheResolver bean like:

如果我用 CacheResolver bean 替换 My CacheManager bean,我会得到同样的错误:

@Bean
public CacheResolver cacheResolver() {
    return new SimpleCacheResolver(new ConcurrentMapCacheManager("entities"));
}

Do I miss something ?

我错过了什么吗?

回答by Pleymor

@herau You were right I had to name the bean ! The problem was that there were another bean "cacheManager", so finally, I didn't annotate Application, and created a configuration as:

@herau 你说得对,我必须给豆子命名!问题是还有一个bean“cacheManager”,所以最后,我没有注释Application,而是创建了一个配置:

@EnableCaching
@Configuration
public class CacheConf{
    @Bean(name = "springCM")
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager("entities");
    }
}

in MyEntityRepository:

MyEntityRepository

    @Cacheable(value = "entities", cacheManager = "springCM")
    MyEntity findByName(String name);

回答by Aliya

In my case the Spring Boot library was old, and there was no way to easily upgrade it. So I used EHCache 2 version, and it worked in my application. Here is a project I found useful to refer to: https://github.com/TechPrimers/spring-ehcache-example/blob/master/src/main/resources/ehcache.xml

就我而言,Spring Boot 库很旧,无法轻松升级它。所以我使用了 EHCache 2 版本,它在我的应用程序中工作。这是我发现有用的一个项目:https: //github.com/TechPrimers/spring-ehcache-example/blob/master/src/main/resources/ehcache.xml