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

Spring Boot Cassandra多模块项目依赖注入失败问题求助

Hey there, let's tackle this issue you're facing after splitting your Spring Boot 2.0.4 + Cassandra project into multi-modules. You've got two key problems here: a missing Guava dependency causing that NoClassDefFoundError, and incorrect repository scanning config that's stopping Spring from finding your PersonRepository. Let's fix them one by one:

1. Resolve the NoClassDefFoundError for Guava's FutureFallback

The root of this error is that the Guava library (a required transitive dependency of the Cassandra Java Driver) isn't making it into your runtime classpath after splitting into modules. While Spring Boot's Cassandra starter usually handles this, multi-module setups can sometimes break dependency transitivity.

Fix Steps:

Explicitly add the Guava dependency to your service module's build file to ensure it's available:

  • For Maven (pom.xml):
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>25.1-jre</version> <!-- Matches Cassandra Driver 3.4.0 (used by Spring Boot 2.0.4) -->
    </dependency>
    
  • For Gradle (build.gradle):
    implementation 'com.google.guava:guava:25.1-jre'
    

Note: Stick to version 25.1-jre here because Spring Boot 2.0.4 uses Cassandra Driver 3.4.0, which relies on this specific Guava version to avoid conflicts.

2. Fix PersonRepository Scanning

Your current Application class uses @ComponentScan and @EntityScan, but you're missing the critical annotation for Spring Data Cassandra repositories: @EnableCassandraRepositories. This is why Spring can't locate and create the PersonRepository bean for injection.

Updated Application Class:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SpringBootApplication(scanBasePackages = "com.mayank") // Replaces explicit @ComponentScan with built-in support
@EnableCassandraRepositories(basePackages = "com.mayank.repository1") // Target your repository package directly
@EntityScan(basePackages = "com.mayank.resources") // Keep this for your Cassandra entities
public class Application extends SpringBootServletInitializer {
    private static final Logger log = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args); // Simplified run call
    }
}

Key Notes:

  • @SpringBootApplication already includes @ComponentScan functionality; using scanBasePackages ensures all your components (services, controllers, etc.) in com.mayank are scanned.
  • @EnableCassandraRepositories must point directly to the package where PersonRepository lives (com.mayank.repository1)—don't just use the parent package, as this ensures Spring picks up all repository interfaces in that module.

Quick Additional Checks

  1. Verify Repository Interface: Make sure your PersonRepository is correctly extending Spring Data Cassandra's repository:
    import org.springframework.data.cassandra.repository.CassandraRepository;
    import com.mayank.resources.Person; // Your Cassandra entity class
    
    public interface PersonRepository extends CassandraRepository<Person, YourKeyType> {
        // Custom query methods if needed
    }
    
  2. Module Dependency: Ensure the module containing PersonRepository is declared as a dependency in your service module's build file. For Maven:
    <dependency>
        <groupId>com.mayank</groupId>
        <artifactId>your-repository-module-name</artifactId>
        <version>your-module-version</version>
    </dependency>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 14:32:31