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

Spring Boot 3.5.5集成EhCache 3.10.8时配置expiry标签报错的问题求助

Spring Boot 3.5.5集成EhCache 3.10.8时配置expiry标签报错的问题求助

问题分析

你遇到的这个错误是因为EhCache 3的XML配置对节点顺序有严格要求,<expiry>标签的位置不符合Schema定义的顺序规则。从错误提示能看出来,当前位置(<resources>之后)期望的是heap-store-settings、disk-store-settings这类节点,而不是<expiry>。

解决方案

你只需要调整<expiry>标签的位置,把它放到<resources>标签之前,作为<cache>的直接子节点即可;或者也可以把过期策略配置到缓存模板里,让所有使用该模板的缓存继承这个规则。

方案1:在单个缓存中调整<expiry>位置

修改后的ehcache.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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="tokenCache" uses-template="defaultTemplate">
    <!-- 将expiry放到resources之前 -->
    <expiry>
      <ttl unit="minutes">30</ttl>
    </expiry>
    <resources>
      <heap unit="entries">1</heap>
    </resources>
  </cache>
  <cache-template name="defaultTemplate">
    <key-type>java.lang.String</key-type>
    <value-type>java.lang.String</value-type>
    <heap unit="entries">1</heap>
  </cache-template>
</config>

方案2:将过期策略配置到缓存模板中(推荐,适合多缓存复用规则)

如果你的多个缓存都需要相同的过期策略,把<expiry>放到<cache-template>里会更方便:

<?xml version="1.0" encoding="UTF-8"?>
<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="tokenCache" uses-template="defaultTemplate">
    <resources>
      <heap unit="entries">1</heap>
    </resources>
  </cache>
  <cache-template name="defaultTemplate">
    <key-type>java.lang.String</key-type>
    <value-type>java.lang.String</value-type>
    <!-- 把expiry配置到模板中 -->
    <expiry>
      <ttl unit="minutes">30</ttl>
    </expiry>
    <heap unit="entries">1</heap>
  </cache-template>
</config>

补充说明

EhCache 3的<cache>节点子元素的合法顺序大致是:description → key-type → value-type → expiry → resources → 各类store-settings → 其他配置。只要遵循这个顺序,就能避免这类Schema校验错误啦~

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 08:43:06