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

SpringBoot应用启动报错:缺失cacheManager Bean,求EhCache无XML配置方案

解决SpringBoot启动报错及EhCache无XML配置方案

1. 解决启动报错:找不到cacheManager bean

报错核心原因是代码中已使用缓存注解(如@Cacheable),但Spring容器未初始化CacheManager实例,按以下两步修复:

1.1 开启缓存支持

在SpringBoot启动类上添加@EnableCaching注解,触发Spring缓存自动配置逻辑:

@SpringBootApplication
@EnableCaching
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

1.2 确认依赖完整性

检查pom.xml中是否包含完整的缓存依赖(SpringBoot 2.x及以上版本):

<dependencies>
    <!-- Spring缓存启动器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <!-- EhCache核心依赖 -->
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
</dependencies>

2. 用application.yaml完成EhCache无XML配置

SpringBoot支持直接通过yaml配置EhCache,无需传统的ehcache.xml文件,提供两种配置方式:

2.1 独立配置文件方式

在src/main/resources下创建ehcache.yaml(或ehcache.xml),然后在application.yaml中指定配置路径:

spring:
  cache:
    type: ehcache
    ehcache:
      config: classpath:ehcache.yaml

ehcache.yaml示例配置(对应数据库表的缓存规则):

ehcache:
  diskStore:
    path: java.io.tmpdir/ehcache-db-cache
  defaultCache:
    maxEntriesLocalHeap: 10000
    eternal: false
    timeToIdleSeconds: 180
    timeToLiveSeconds: 360
    memoryStoreEvictionPolicy: LRU
  # 针对特定数据库表的自定义缓存
  caches:
    userTableCache:
      maxEntriesLocalHeap: 500
      eternal: false
      timeToIdleSeconds: 300
      timeToLiveSeconds: 600
      memoryStoreEvictionPolicy: LRU

2.2 内嵌配置方式(SpringBoot 2.3+支持)

直接在application.yaml中内嵌EhCache配置,无需额外文件:

spring:
  cache:
    type: ehcache
    ehcache:
      config: |
        <?xml version="1.0" encoding="UTF-8"?>
        <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
          <diskStore path="java.io.tmpdir/ehcache"/>
          <defaultCache
                  maxEntriesLocalHeap="10000"
                  eternal="false"
                  timeToIdleSeconds="180"
                  timeToLiveSeconds="360"
                  memoryStoreEvictionPolicy="LRU"/>
          <cache name="userTableCache"
                 maxEntriesLocalHeap="500"
                 eternal="false"
                 timeToIdleSeconds="300"
                 timeToLiveSeconds="600"/>
        </ehcache>

3. 业务代码中使用缓存

在数据库查询方法上添加@Cacheable注解,指定对应缓存名称即可:

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Cacheable(value = "userTableCache", key = "#userId")
    public User getDbUserById(Long userId) {
        return userMapper.selectUserById(userId);
    }
}

4. 验证生效

启动应用后,首次调用查询方法会执行数据库操作,后续相同参数调用将直接返回缓存数据;同时启动日志中会出现EhCacheCacheManager初始化成功的日志,说明cacheManager bean已创建,启动报错解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 16:32:41