简体   繁体   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 ). 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 ). Such repository is not a JpaRepository because I want to avoid JPA in this stage of the project, and only resorting to straight SQL queries, so I'm using Repository / @EnableJdbcRepositories :这样的存储库不是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>

}

Such service is then @Autowire d in com.myapp.engine.service.MyService , but autowiring fails because no beans for MyRepository are found:然后此类服务在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

Reading Spring Data JDBC reference it looks like this approach should be valid.阅读 Spring 数据 JDBC 参考,看起来这种方法应该是有效的。 What am I doing wrong?我究竟做错了什么?

My issue was that I had multiple Repository candidates (because I also had spring-boot-starter-redis as a dependency).我的问题是我有多个存储库候选者(因为我也有spring-boot-starter-redis作为依赖项)。 Removing it, or annotating with @Table my entity fixed the issue.删除它,或者用@Table注释我的实体解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM