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:
@SpringBootApplicationalready includes@ComponentScanfunctionality; usingscanBasePackagesensures all your components (services, controllers, etc.) incom.mayankare scanned.@EnableCassandraRepositoriesmust point directly to the package wherePersonRepositorylives (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
- Verify Repository Interface: Make sure your
PersonRepositoryis 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 } - Module Dependency: Ensure the module containing
PersonRepositoryis 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

