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

Spring Boot 3中ehcache.xml配置文件无法加载问题求助

Spring Boot 3中Ehcache配置文件无法加载问题排查

问题现象

启动应用时收到警告:

Missing cache[dictionary] was created on-the-fly. The created cache will use a provider-specific default configuration: make sure you defined one. You can disable this warning by setting 'hibernate.javax.cache.missing_cache_strategy' to 'create'.

无论ehcache.xml是否放置在/src/main/resources/目录下,系统都会动态创建dictionary和hotel缓存,完全不使用配置文件中的自定义设置。添加logging.level.net.sf.ehcache=DEBUG日志配置后,调试日志确认了动态创建缓存的行为。

现有配置

依赖配置

<dependency>
    <groupId>org.hibernate.orm</groupId>
    <artifactId>hibernate-jcache</artifactId>
</dependency>
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>

application.properties配置

hibernate.cache.use_second_level_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.jcache.internal.JCacheRegionFactory
hibernate.javax.cache.uri=ehcache.xml
hibernate.javax.cache.provider=org.ehcache.jsr107.EhcacheCachingProvider

ehcache.xml配置

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.ehcache.org/v3"
        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd">

    <cache alias="dictionary">
        <expiry>
            <tti unit="hours">4</tti>
        </expiry>
        <heap>10000</heap>
    </cache>

    <cache alias="hotel">
        <expiry>
            <tti unit="hours">24</tti>
        </expiry>
        <heap>200</heap>
    </cache>
</config>

问题原因

  1. 配置属性错误:hibernate.javax.cache.uri不是Spring Boot 3中指定JCache配置文件的正确属性,Hibernate无法通过该属性定位到ehcache.xml。
  2. 日志包不匹配:使用的net.sf.ehcache日志包对应Ehcache 2版本,而当前依赖的是Ehcache 3,导致无法输出正确的配置加载日志,无法排查加载失败原因。
  3. 缓存名称匹配问题:若实体类上的缓存注解名称(如@Cacheable的value)与ehcache.xml中的alias不一致,也会触发动态创建缓存的逻辑。

解决方案

  1. 修正配置属性:将application.properties中的hibernate.javax.cache.uri=ehcache.xml替换为spring.jcache.config=classpath:ehcache.xml,该属性是Spring Boot标准的JCache配置文件指定方式。
  2. 调整日志配置:改为logging.level.org.ehcache=DEBUG,这样可以输出Ehcache 3的加载日志,确认配置文件是否被成功读取。
  3. 校验缓存名称一致性:确保实体类缓存注解中的名称与ehcache.xml里的alias完全一致(大小写敏感)。
  4. 检查版本兼容性:确认Spring Boot 3版本与Ehcache、Hibernate JCache版本兼容,建议使用Spring Boot官方依赖管理的版本,避免版本冲突。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 04:35:17