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

Spring项目升级Cassandra Java Driver 3.11.3至4.0.0遇问题求助

解决方案:Cassandra 4.0.0 升级兼容问题修复

针对你遇到的TypeCodec找不到、Spring自动装配Mock失效的问题,核心原因是版本兼容性不匹配,以下是具体修复步骤:

1. 匹配Achilles与Cassandra Driver 4.0的兼容版本

Cassandra Driver 4.x重构了核心API(比如TypeCodec的包路径从com.datastax.driver.core迁移到com.datastax.oss.driver.api.core.type.codec),旧版本的Achilles无法适配这个变化。你需要升级Achilles到支持Driver 4.x的版本,同时确保兼容Spring 4.0:

推荐使用Achilles 6.0.0,它原生支持Cassandra Driver 4.x,且对Spring 4.0的兼容性较好。调整POM依赖如下:

<dependency>
    <groupId>info.archinnov</groupId>
    <artifactId>achilles-core</artifactId>
    <version>6.0.0</version>
    <!-- 无需手动排除Driver,Achilles 6.0.0默认依赖兼容的Driver版本 -->
</dependency>

<dependency>
    <groupId>com.datastax.cassandra</groupId>
    <artifactId>cassandra-driver-core</artifactId>
    <version>4.0.0</version>
</dependency>

2. 修复TypeCodec引用错误

如果你的代码中直接引用了TypeCodec,需要将包路径从旧的:

import com.datastax.driver.core.TypeCodec;

替换为Driver 4.x的新路径:

import com.datastax.oss.driver.api.core.type.codec.TypeCodec;

3. 解决Spring Mock自动装配问题

  • 检查测试类的注解配置:确保使用Spring 4.0兼容的测试运行器和注解,比如:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:spring-config.xml")
    public class YourTestClass {
        @Autowired
        @Mock
        private YourService mockService;
        
        // 测试逻辑
    }
    
  • 确保Mockito版本与Spring 4.0兼容:推荐使用Mockito 1.10.19或2.2.x版本,避免版本过高导致的注解冲突。

4. 处理依赖冲突(如Guava)

Cassandra Driver 4.x依赖的Guava版本可能与Spring 4.0自带的版本冲突,可通过排除依赖统一版本:

<dependency>
    <groupId>info.archinnov</groupId>
    <artifactId>achilles-core</artifactId>
    <version>6.0.0</version>
    <exclusions>
        <exclusion>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- 引入Driver 4.0兼容的Guava版本 -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>28.1-jre</version>
</dependency>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 06:42:34