首页 > 解决方案 > X 中构造函数的参数 0 需要找不到类型为“Y”的 bean

问题描述

每当我运行我的 Spring Boot 应用程序以连接到 AWS 数据库时,我都会收到错误消息

services.UserService 中构造函数的参数 0 需要一个无法找到的“repositories.UserRepository”类型的 bean。

我目前的项目结构是这样的:

在此处输入图像描述

以下是各个文件:

用户.kt:

package entities

import org.springframework.data.annotation.Id
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Table

@Entity
@Table(name = "users")
data class User(
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    val id: Long?,
    val name: String
)

用户存储库.kt:

package repositories

import entities.User
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.CrudRepository
import org.springframework.stereotype.Component
import org.springframework.stereotype.Repository

@Repository
interface UserRepository : CrudRepository<User, Long>{
    @Query("SELECT * FROM users")
    fun getAllUsers(): List<User>
}

用户服务.kt:

package services

import entities.User
import org.springframework.stereotype.Service
import repositories.UserRepository

@Service
class UserService(val database: UserRepository) {

    fun getAllUsers(): List<User> = database.getAllUsers()

    fun post(user: User){
        database.save(user)
    }
}

用户控制器.kt:

package controllers

import entities.User
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RestController
import services.UserService

@RestController
class UserController(val userService: UserService) {

    @GetMapping
    fun index() = "TEST"

    @PostMapping("/users/add")
    fun addUser(user: User){
        userService.post(user)
    }

    @GetMapping("/users/all")
    fun getAllUsers() = userService.getAllUsers()
}

TutortekApplication.kt:

package com.karbal.tutortek

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.ComponentScan

@SpringBootApplication(scanBasePackages = ["controllers", "entities", "repositories", "services"])
class TutortekApplication

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

我缺少什么来完成这项工作?

标签: spring-bootkotlin

解决方案


将包“控制器”、“实体”、“存储库”和“服务”移动到com.karbal.tutortek. 有了这个,你可以摆脱scanBasePackages = ["controllers", "entities", "repositories", "services"]并只使用@SpringBootApplication,如下所示:

package com.karbal.tutortek

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.ComponentScan

@SpringBootApplication
class TutortekApplication

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

@SpringBootApplication用它们的默认属性封装@Configuration@EnableAutoConfiguration@ComponentScan注释。的默认值@ComponentScan意味着@ComponentScan扫描所使用的包上的所有子包。这就是为什么在项目的基础包中包含主类通常是一个很好的做法。


推荐阅读