Kotlin Field required a bean of type that could not be found 错误
Posted
技术标签:
【中文标题】Kotlin Field required a bean of type that could not be found 错误【英文标题】:Kotlin Field required a bean of type that could not be found error 【发布时间】:2021-10-29 17:47:06 【问题描述】:我使用 KOTLIN / SPRING BOOT(2.5.4) / MAVEN / MARIA DB 我遇到了这样的错误,所以我想我必须找到 make bean。 但我找不到我错过的东西。你能帮帮我吗?
> 2021-08-31 13:45:25.923 WARN 7696 --- [ restartedMain]
> ConfigServletWebServerApplicationContext : Exception encountered
> during context initialization - cancelling refresh attempt:
> org.springframework.beans.factory.UnsatisfiedDependencyException:
> Error creating bean with name 'userController': Unsatisfied dependency
> expressed through field 'userService'; nested exception is
> org.springframework.beans.factory.UnsatisfiedDependencyException:
> Error creating bean with name 'userServiceImpl': Unsatisfied
> dependency expressed through field 'userRepository'; nested exception
> is org.springframework.beans.factory.NoSuchBeanDefinitionException: No
> qualifying bean of type
> 'com.example.test0831.repository.UserRepository' available: expected
> at least 1 bean which qualifies as autowire candidate. Dependency
> annotations:
> @org.springframework.beans.factory.annotation.Autowired(required=true)
> 2021-08-31 13:45:25.925 INFO 7696 --- [ restartedMain]
> o.apache.catalina.core.StandardService : Stopping service [Tomcat]
> 2021-08-31 13:45:25.933 INFO 7696 --- [ restartedMain]
> ConditionEvaluationReportLoggingListener :
>
> Error starting ApplicationContext. To display the conditions report
> re-run your application with 'debug' enabled. 2021-08-31 13:45:25.947
> ERROR 7696 --- [ restartedMain]
> o.s.b.d.LoggingFailureAnalysisReporter :
>
> *************************** APPLICATION FAILED TO START
> ***************************
>
> Description:
>
> Field userRepository in com.example.test0831.service.UserServiceImpl
> required a bean of type
> 'com.example.test0831.repository.UserRepository' 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.example.test0831.repository.UserRepository' in your
> configuration.
我的项目是 科特林 com.example.test0831 └ 控制器 └ 用户控制器 └ 实体 └ 用户 └ 仓库 └ 用户存储库 └ 服务 └ 用户服务 └ 用户服务实现
包 com.example.test0831.controller.UserController
import com.example.test0831.entity.User
import com.example.test0831.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@Controller
class UserController
@Autowired
lateinit var userService: UserService
@GetMapping("users")
fun getUserList(): List<User>
return userService.getUserList()
包 com.example.test0831.entity.User
data class User(var id: Int, var name: String, var phone: String, var address: String)
包 com.example.test0831.repository.UserRepository
import org.apache.ibatis.annotations.*
import com.example.test0831.entity.User
import org.springframework.stereotype.Repository
@Mapper
@Repository
interface UserRepository
@Select("Select * FROM user;")
fun selectUserList(): List<User>
***package com.example.test0831.service.UserService***
import com.example.test0831.entity.User
interface UserService
fun getUserList(): List<User>
包 com.example.test0831.service.UserServiceImpl
import com.example.test0831.entity.User
import com.example.test0831.repository.UserRepository
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
@Service
class UserServiceImpl: UserService
@Autowired
private lateinit var userRepository: UserRepository
@Override
override fun getUserList(): List<User> = userRepository.selectUserList()
application.properties
spring.datasource.driverClassName=org.mariadb.jdbc.Driver spring.datasource.url=jdbc:mariadb://localhost:3306/userinfo?useUnicode=true&characterEncoding=utf-8 spring.datasource.username=root spring.datasource.password=password spring.jpa.show-sql=true
【问题讨论】:
【参考方案1】:您应该定义您的 UserRepository 以扩展任何 spring JPA repository 接口,例如CrudRepository
或 JPARepository
interface UserRepository : CrudRepository<User, Long>
@Select("Select * FROM user;")
fun selectUserList(): List<User>
【讨论】:
感谢您的回答!我尝试像您的代码一样使用 CrudRepository以上是关于Kotlin Field required a bean of type that could not be found 错误的主要内容,如果未能解决你的问题,请参考以下文章
(Kotlin 中的 Moshi)@Json 与 @field:Json
Field amqpTemplate in * required a single bean, but 3 were found:
springboot 报错Field XXX required a bean of type XXX that could not be found.