【问题标题】:CrudRepository interface - bean not foundCrudRepository 接口 - 找不到 bean
【发布时间】:2020-06-30 14:59:42
【问题描述】:

我正在像这样使用 CrudRepository

@Repository
public interface Reports extends CrudRepository<Report, UUID> {

}

当我尝试将Reports 存储库注入这样的服务时

@Component
@AllArgsConstructor
class ReportsService {

    private final Reports reports;

// ...
}

我来了

Parameter 0 of constructor in ReportsService required a bean of type 'Reports' that could not be found.
Consider defining a bean of type Reports in your configuration.

我的 gradle 依赖文件是这样的

    implementation(
            'org.flywaydb:flyway-core',
            'org.springframework.boot:spring-boot-starter-data-jpa',
            'com.vladmihalcea:hibernate-types-52:2.3.3',
            'org.springframework.boot:spring-boot-starter',
            'org.springframework.boot:spring-boot-starter-web',
            'com.google.code.gson:gson:2.8.5',
            'com.opencsv:opencsv:4.1',
            'org.springframework.data:spring-data-mongodb',
            'org.springframework.boot:spring-boot-starter-data-jdbc'
    )

什么可能配置错误?

【问题讨论】:

  • 你的界面是用@Repository注解的吗?
  • @LuisIñesta 我现在添加它没有效果
  • 你有@ComponentScan@EnableAutoconfiguration 吗?尝试在ReportsService 之上添加@Import(Reports.class)
  • Spring Data 接口不需要该注释。问题是很可能您的项目中包含多个 Spring Data 实现,因此需要指定存储库基本类型(例如 JpaRepository)。我怀疑您是否需要所有依赖项并且可以删除 MongoDB 和 JDBC。
  • @chrylis-cautiouslyoptimistic- 这很可能是原因。如果我需要同时使用 postgresql (spring-boot-starter-data-jpa) 和 mongodb 怎么办?

标签: java spring-boot spring-data-mongodb


【解决方案1】:

好的,所以解决方案是摆脱 org.springframework.data:spring-data-mongodb 并使用不同的东西,在我的例子中,我使用了 mongodb Morphia。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2021-08-21
    • 2019-11-14
    相关资源
    最近更新 更多