Spring Boot 2.0 无法使用 Caffeine 作为缓存提供程序

标签 spring spring-boot

我使用 Web 和缓存依赖项创建 Spring Boot 2.0 Starter 项目:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

然后我更新了 Spring bootstrap 类来测试 REST 服务缓存:

@SpringBootApplication
@EnableCaching
@RestController
@RequestMapping
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @GetMapping
    @Cacheable("hello")
    public String hello() {
        return "1";
    }
}

并在 application.properties 中指定简单的缓存提供程序:

spring.cache.type=simple

一切都按预期进行。然后我添加了 Caffeine 依赖项并更改了缓存类型:

<dependency>
    <groupId>com.github.ben-manes.caffeine</groupId>
    <artifactId>caffeine</artifactId>
</dependency>

spring.cache.type=caffeine

在该应用程序因异常而无法启动之后:

Caused by: java.lang.IllegalArgumentException: No cache manager could be auto-configured, 检查你的配置(缓存类型是'CAFFEINE') 在 org.springframework.util.Assert.notNull(Assert.java:193) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE] 在 org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration$CacheManagerValidator.checkHasCacheManager(CacheAutoConfiguration.java:151) ~[spring-boot-autoconfigure-2.0.0.RELEASE.jar:2.0.0.RELEASE]

我试图在 application.properties 中提供缓存名称,但没有帮助。

spring.cache.cache-names=hello

请指教。

最佳答案

添加如下依赖:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>5.0.8.RELEASE</version>
</dependency>

关于Spring Boot 2.0 无法使用 Caffeine 作为缓存提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49362975/

相关文章:

java - 如何向 Spring MVC Controller 方法添加自定义安全注释

Java Spring Boot 配置和组件

java - 如何在 Web 服务响应中将 json 响应作为 block 返回?

java - Spring Boot REST 应用程序使用 Jasper Report Server rest-api 时出现的问题

java - 使用 Postgres 获取 "Duplicate key value violates unique constraint"

mysql - 错误的 SQL 语法...未找到列 "COUNT(status)"

Spring 批处理不处理所有记录

java - 将 Spring Boot 与 Spring Security 集成

Spring Retry 与 Hystrix

java - 测试 Spring MVC 时不断收到 404 错误