簡體   English   中英

從另一個模塊引用存儲庫時出現 UnsatisfiedDependencyException

[英]UnsatisfiedDependencyException when referencing Repository from another module

I have a Kotlin Spring Boot application ( com.myapp.webapp.Application ) which uses a Repository bean defined in an external library ( com.myapp.engine.MyRepository ). 這樣的存儲庫不是JpaRepository因為我想在項目的這個階段避免 JPA ,並且只采用直接的 SQL 查詢,所以我使用Repository / @EnableJdbcRepositories

package com.myapp.webapp

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories

@SpringBootApplication(scanBasePackages = [
    "com.myapp.webapp",
    "com.myapp.engine",
])
@EnableJdbcRepositories(basePackages = ["com.myapp.engine"])
class MyApplication

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

package com.myapp.engine.repository

import com.myapp.engine.MyType
import org.springframework.data.jdbc.repository.Query
import org.springframework.data.repository.Repository
import java.util.*

interface MyRepository: Repository<MyType, Int> {

    @Query("select * from my_type", nativeQuery=true)
    fun findAll(): List<MyType>

}

然后此類服務在com.myapp.engine.service.MyService中為@Autowire d,但自動裝配失敗,因為找不到MyRepository的 bean:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.myapp.engine.service.MyService required a bean of type 'com.myapp.engine.repository.MyRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.webapp.engine.repository.MyRepository' in your configuration.


Process finished with exit code 1

閱讀 Spring 數據 JDBC 參考,看起來這種方法應該是有效的。 我究竟做錯了什么?

我的問題是我有多個存儲庫候選者(因為我也有spring-boot-starter-redis作為依賴項)。 刪除它,或者用@Table注釋我的實體解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM