1

I use KOTLIN / SPRING BOOT(2.5.4) / MAVEN / MARIA DB I got an error like this, so I thought I have to found make bean. But I couldn't found What I missed. Can you help me...?

> 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.

My Project is Kotlin com.example.test0831 └ controller └ UserController └ entity └ User └ repository └ UserRepository └ service └ UserService └ UserServiceImpl

package 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()
    }
}

package com.example.test0831.entity.User

data class User(var id: Int, var name: String, var phone: String, var address: String)

package 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>
}

package 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
pijyuuuuu
  • 11
  • 1

1 Answers1

0

You should be defining your UserRepository to extend any of the spring JPA repository interface e.g. CrudRepository or JPARepository

interface UserRepository  : CrudRepository<User, Long> {

     @Select("Select * FROM user;")
     fun selectUserList(): List<User>
 }
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
  • Thank you for you answer! I try to use CrudRepository like your code but I got same error message... T^T Is my package wrong..?TT I think.. I have to try again.. 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) – pijyuuuuu Aug 31 '21 at 05:37
  • Have a look here - https://stackoverflow.com/questions/55064074/why-is-bean-not-found-during-spring-boot – Ashishkumar Singh Aug 31 '21 at 06:07