spring-boot - 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)
}
我缺少什么来完成这项工作?
解决方案
将包“控制器”、“实体”、“存储库”和“服务”移动到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
扫描所使用的包上的所有子包。这就是为什么在项目的基础包中包含主类通常是一个很好的做法。
推荐阅读
- pyparsing - 在python中解析引发异常
- android - Android 语音识别器
- laravel - Laravel Eloquent - 通过其他表连接 2 个表
- c# - 将 TabItem 的 Header 绑定到该选项卡 Content 的属性
- java - JSON 路径使用 JSON 路径从父对象和子对象中提取特定字段/数组
- javascript - 无法比较包含绑定到我在 React 中的类的函数的对象
- angular - 如何在 Angular 6 项目中使用 ng2-currency-mask
- bayesian - BUGS 模型中的错误
- c++ - 我如何初始化 LPVOID
- python - 格式化 csv 数据并将其写入文本文件以进行机器学习