![](/img/trans.png)
[英]Spring data r2dbc: Problem connecting with mysql - 'r2dbcEntityTemplate' could not be found
[英]Repository could not be found Spring R2DBC
在我的代碼中,我遇到了這個奇怪的問題,在我的 controller 中無法識別我的一個存儲庫並在啟動期間拋出此錯誤。
com.test.pack.controller.ModulesController 中構造函數的參數 0 需要找不到類型為“com.test.pack.repository.ModuleRepository”的 bean。
這是我的 controller
@RestController
@RequestMapping("/modules")
class ModulesController(private val moduleRepository: ModuleRepository) {
@GetMapping
suspend fun getAllModules(): = Response(moduleRepository.findAllPurchasableModule().toList())
}
這是我的存儲庫
interface ModuleRepository: Repository<Module, Int> {
suspend fun getByAccountId(accountId: Long): Flow<Module>
suspend fun findAllPurchasableModule(): Flow<Module>
}
並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()
}
}
這是我的文件夾結構
com
test
pack
controller
ModulesController
repository
ModuleRepository
UserRepository
impl
ModuleRepositoryImpl
UserRepositoryImpl
我也在為User
存儲庫做同樣的事情,它正在工作。 但是,如果我將@Repository("moduleRepository")
放在impl
class 之上,那么它就可以工作。 但我不明白這個 ModuleRepository 有什么特別之處,與其他概念相同的概念不適用於這個。
作為最佳實踐,您應該始終使用@Repository 和其他注釋,如@Component、@Service 等,以便在類路徑掃描期間輕松檢測到 Spring,並將其作為 bean 注冊到應用程序上下文中。
User Repository 工作的原因可能是因為(不包括 moduleRepository )它是當前代碼中存在的唯一 Repository 類型 bean。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.