Spring Context Support 6.0.x移除ehcache包,求替代方案
解决Spring Context Support 6.0.x中Ehcache包移除的替代方案
核心说明
Spring Context Support 6.0.x确实完全移除了内置的Ehcache集成模块,这是Spring 6.x架构调整的一部分——官方不再维护第三方缓存的内置整合,转而引导用户使用各缓存厂商提供的官方适配包,以获得更及时的版本更新和功能支持。
替代方案选项
1. 直接使用Ehcache官方的Spring整合依赖
Ehcache提供了专门适配Spring的依赖,可完全替代原spring-context-support中的Ehcache模块。只需在项目构建文件中添加以下依赖:
Maven
<dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache-spring</artifactId> <version>3.10.8</version> <!-- 建议使用最新稳定版 --> </dependency>
Gradle
implementation 'org.ehcache:ehcache-spring:3.10.8'
该依赖会自动处理与Spring框架的整合,包括CacheManager配置、缓存注解支持等核心功能。
2. 基于Spring Cache抽象搭配Ehcache 3
若项目已使用Spring Cache抽象(如@Cacheable等注解),可直接配置Ehcache 3作为底层缓存实现:
- 添加核心依赖:
<dependency> <groupId>org.ehcache</groupId> <artifactId>ehcache</artifactId> <version>3.10.8</version> </dependency> <dependency> <groupId>javax.cache</groupId> <artifactId>cache-api</artifactId> </dependency>
- 在Spring配置类中绑定Ehcache配置:
@Configuration @EnableCaching public class CacheConfig { @Bean public JCacheCacheManager cacheManager() { CachingProvider cachingProvider = Caching.getCachingProvider(); CacheManager cacheManager = cachingProvider.getCacheManager( getClass().getResource("/ehcache.xml").toURI(), getClass().getClassLoader() ); return new JCacheCacheManager(cacheManager); } }
- 编写
ehcache.xml配置文件定义缓存策略即可。
3. 迁移至其他缓存实现
若无需强依赖Ehcache,也可选择Spring官方推荐的替代方案:
- Caffeine:高性能本地缓存,Spring Boot默认集成,配置简单
- Redis:分布式缓存,适合集群部署场景
内容的提问来源于stack exchange,提问作者Kaivalya Srivastava
相关产品推荐
相关产品推荐

