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

Spring Boot 3.2.4中使用CoroutineCrudRepository启动报错求助

解决Spring Boot中CoroutineCrudRepository Bean未找到的问题

你的Spring Boot项目启动时触发以下错误:

APPLICATION FAILED TO START
描述:com.michaelrenops.demo.service.AccountService构造函数的第0个参数需要类型为'com.michaelrenops.demo.repository.AccountRepository'的Bean,但未找到。
操作建议:考虑在配置中定义该类型的Bean。进程退出码为1。

核心问题及解决方案如下:

1. 修正实体类的注解错误

你的AccountModel使用了错误的@Documents注解,Spring Data MongoDB识别的实体注解是**@Document**(单数形式),这是导致Repository Bean无法生成的直接原因。修正后的实体代码:

@Document("accounts")
class AccountModel(
    @Id
    var id: String? = null,

    val name: String,

    @CreatedDate
    var createdAt: Instant? = null,

    @LastModifiedDate
    var updatedAt: Instant? = null
)

2. 确认组件扫描范围

确保DemoApplication主类位于根包com.michaelrenops.demo下,Spring会自动扫描该包及其子包内的所有组件。如果主类不在根包,需手动指定扫描范围:

@SpringBootApplication(scanBasePackages = ["com.michaelrenops.demo"])
@EnableMongoAuditing // 启用@CreatedDate/@LastModifiedDate审计注解支持
class DemoApplication

fun main(args: Array<String>) {
    runApplication<DemoApplication>(*args)
}

3. 显式启用Reactive MongoDB Repository扫描

若Repository接口仍未被识别,可在主类添加@EnableReactiveMongoRepositories注解,明确指定Repository所在包:

@SpringBootApplication
@EnableMongoAuditing
@EnableReactiveMongoRepositories(basePackages = ["com.michaelrenops.demo.repository"])
class DemoApplication

4. 验证依赖配置

检查pom.xml中Reactive MongoDB依赖是否正确引入:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>

确保该依赖未被错误排除,版本与Spring Boot 3.2.4兼容。

内容的提问来源于stack exchange,提问作者Michael Reno Pratama Setyawan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 19:47:15