You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot应用报错:找不到org.springframework.cache.CacheManager类型的Bean

解决Spring Boot中找不到CacheManager Bean的问题

错误信息:找不到类型为org.springframework.cache.CacheManager的Bean。建议操作:考虑在配置中定义一个org.springframework.cache.CacheManager类型的Bean。

常见解决方法:

1. 启用Spring Boot自动缓存配置

在你的Spring Boot启动类上添加@EnableCaching注解,Spring Boot会根据classpath中的依赖自动适配并创建对应的CacheManager Bean:

@SpringBootApplication
@EnableCaching
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}
  • 无第三方缓存依赖时,默认使用ConcurrentMapCacheManager(本地内存缓存);
  • 若存在Redis、Caffeine等缓存依赖,会自动初始化对应类型的CacheManager。

2. 自定义CacheManager Bean

如果需要定制缓存规则(如指定缓存名称、设置过期时间),可以手动编写配置类:

import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.cache.annotation.EnableCaching;

@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public CacheManager cacheManager() {
        // 指定要创建的缓存名称
        ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager("userCache", "orderCache");
        // 若使用Redis等支持过期的缓存,可在此添加过期时间等配置
        return cacheManager;
    }
}

3. 确认缓存依赖完整性

如果使用第三方缓存(如Redis、Caffeine),需确保已引入对应的Spring Boot Starter依赖:

  • Maven(Redis示例):
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
  • Gradle(Caffeine示例):
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'com.github.ben-manes.caffeine:caffeine'

内容的提问来源于stack exchange,提问作者Arabinda Behera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 19:22:43