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 接口,例如CrudRepositoryJPARepository

interface UserRepository  : CrudRepository<User, Long> 

     @Select("Select * FROM user;")
     fun selectUserList(): List<User>
 

【讨论】:

感谢您的回答!我尝试像您的代码一样使用 CrudRepository 但我收到相同的错误消息... T^T 我的包错了..?TT 我想.. 我必须再试一次.. 描述:com 中的字段 userRepository .example.test0831.service.UserServiceImpl 需要找不到类型为“com.example.test0831.repository.UserRepository”的 bean。注入点有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true) 看看这里 - ***.com/questions/55064074/…

以上是关于Kotlin Field required a bean of type that could not be found 错误的主要内容,如果未能解决你的问题,请参考以下文章

(Kotlin 中的 Moshi)@Json 与 @field:Json

Kotlin field 关键字

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.

Kotlin 中定义类field关键字,主构造函数和次构造函数详解

Kotlin 中定义类field关键字,主构造函数和次构造函数详解