【发布时间】: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