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
相关产品推荐
相关产品推荐

