我使用 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/