[英]Repository could not be found Spring R2DBC
In my code, I ran into this weird issue where one of my repositories is not being recognized in my controller and throwing this error during startup.在我的代码中,我遇到了这个奇怪的问题,在我的 controller 中无法识别我的一个存储库并在启动期间抛出此错误。
Parameter 0 of constructor in com.test.pack.controller.ModulesController required a bean of type 'com.test.pack.repository.ModuleRepository' that could not be found.
com.test.pack.controller.ModulesController 中构造函数的参数 0 需要找不到类型为“com.test.pack.repository.ModuleRepository”的 bean。
This is my controller这是我的 controller
@RestController
@RequestMapping("/modules")
class ModulesController(private val moduleRepository: ModuleRepository) {
@GetMapping
suspend fun getAllModules(): = Response(moduleRepository.findAllPurchasableModule().toList())
}
This is my repository这是我的存储库
interface ModuleRepository: Repository<Module, Int> {
suspend fun getByAccountId(accountId: Long): Flow<Module>
suspend fun findAllPurchasableModule(): Flow<Module>
}
and impl
并
impl
class ModuleRepositoryImpl(private val databaseClient: DatabaseClient) : ModuleRepository {
override suspend fun getByAccountId(accountId: Long): Flow<Module> {
return databaseClient.execute("SELECT * FROM mod WHERE actid = :acct").bind("accountId", acct).asType<Module>().fetch().flow()
}
override suspend fun findAllPurchasableModule(): Flow<Module> {
return databaseClient.execute("SELECT * FROM modules").asType<Module>().fetch().flow()
}
}
and this is my folder structure这是我的文件夹结构
com
test
pack
controller
ModulesController
repository
ModuleRepository
UserRepository
impl
ModuleRepositoryImpl
UserRepositoryImpl
and I am doing the exact same for the User
repository as well and it is working.我也在为
User
存储库做同样的事情,它正在工作。 However, if I put @Repository("moduleRepository")
on top of impl
class then it works.但是,如果我将
@Repository("moduleRepository")
放在impl
class 之上,那么它就可以工作。 But I do not understand what is special about this ModuleRepository that same concept as others are not working for this one.但我不明白这个 ModuleRepository 有什么特别之处,与其他概念相同的概念不适用于这个。
As a best practice, you should always use the @Repository and other annotations like @Component, @Service etc for Spring to easily detect during classpath scanning and register it in the Application context as a bean.作为最佳实践,您应该始终使用@Repository 和其他注释,如@Component、@Service 等,以便在类路径扫描期间轻松检测到 Spring,并将其作为 bean 注册到应用程序上下文中。
The reason why the User Repository is working, might be because (excluding the moduleRepository ) it's the only Repository type bean present in your code currently. User Repository 工作的原因可能是因为(不包括 moduleRepository )它是当前代码中存在的唯一 Repository 类型 bean。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.