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

Spring6中EhCacheManagerFactoryBean替代方案及版本适配疑问

Spring 6 迁移 EhCache 问题解决方案

核心结论

  1. EhCache 2.x 在 Spring 6 中完全不合理:Spring 6 已彻底移除对 EhCache 2.x 的支持,仅适配实现 JSR-107(JCache)规范的 EhCache 3.x,你必须升级 EhCache 版本至 3.x 系列。
  2. 替代组件:Spring 6 通过 JCache 集成模块支持 EhCache 3.x,替代原 org.springframework.cache.ehcache.EhCacheManagerFactoryBean 的是 org.springframework.cache.jcache.JCacheManagerFactoryBean,配合 JCacheCacheManager 实现缓存管理。

具体操作步骤

1. 更新 Maven 依赖

移除原 Spring 对 EhCache 2.x 的支持依赖以及 EhCache 2.x 的依赖,替换为以下内容:

<!-- JCache API(Jakarta 版本,适配 Tomcat 10+) -->
<dependency>
    <groupId>jakarta.cache</groupId>
    <artifactId>jakarta.cache-api</artifactId>
    <version>2.0.1</version> <!-- 适配 Spring 6 的稳定版本 -->
</dependency>
<!-- EhCache 3.x 实现 -->
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>3.10.8</version> <!-- 可根据需求选择最新稳定版 -->
</dependency>
<!-- Spring JCache 集成(若已引入 spring-context 则无需额外添加) -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>6.x.x</version> <!-- 你的 Spring 6 具体版本 -->
</dependency>

2. 修改 XML 配置

替换原 EhCache 2.x 的配置 Bean,改为 JCache 集成的配置:

<!-- 配置 EhCache 3.x 的 CacheManager 工厂 Bean -->
<bean id="jCacheManagerFactory" class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
    <!-- 指定 EhCache 3.x 配置文件路径 -->
    <property name="cacheManagerUri" value="classpath:ehcache.xml"/>
</bean>

<!-- 配置 Spring 缓存管理器,关联 JCache 的 CacheManager -->
<bean id="cacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
    <property name="cacheManager" ref="jCacheManagerFactory"/>
</bean>

3. 升级 EhCache 配置文件

EhCache 3.x 的配置文件格式与 2.x 完全不同,需将原 ehcache.xml 转换为 3.x 格式。示例如下:

原 EhCache 2.x 配置(示例)

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">
    <defaultCache maxEntriesLocalHeap="10000" eternal="false"
                  timeToIdleSeconds="120" timeToLiveSeconds="120"
                  memoryStoreEvictionPolicy="LRU"/>
</ehcache>

转换为 EhCache 3.x 配置

<config xmlns="http://www.ehcache.org/v3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd">
    <!-- 定义默认缓存模板 -->
    <cache-template name="defaultTemplate">
        <heap unit="entries">10000</heap>
        <expiry>
            <tti unit="seconds">120</tti> <!-- 空闲超时 -->
            <ttl unit="seconds">120</ttl> <!-- 存活超时 -->
        </expiry>
        <eviction-policy>LRU</eviction-policy>
    </cache-template>
    <!-- 具体缓存实例,引用模板 -->
    <cache alias="yourCacheName" uses-template="defaultTemplate"/>
</config>

4. 验证配置

确保项目启动时能加载新的缓存配置,若有自定义缓存注解(如 @Cacheable),无需修改注解,Spring 会自动通过新的 cacheManager 处理缓存逻辑。

内容的提问来源于stack exchange,提问作者D.Lm

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 19:15:01