Issue
Whenever I run my Spring Boot app to connect to an AWS database I get the error saying
Parameter 0 of constructor in services.UserService required a bean of type 'repositories.UserRepository' that could not be found.
My current project structure is like this:
Here are the individual files:
User.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
)
UserRepository.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>
}
UserService.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)
}
}
UserController.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)
}
What am I missing to make this work?
Solution
Move the packages "controllers", "entities", "repositories" and "services" to com.karbal.tutortek
. With this you may get rid of scanBasePackages = ["controllers", "entities", "repositories", "services"]
and use only @SpringBootApplication as follows:
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
encapsulates @Configuration
, @EnableAutoConfiguration
, and @ComponentScan
annotations with their default attributes. The default value for @ComponentScan
means that all the sub packages on the package the @ComponentScan
is used are scanned. That is why it is usually a good practice to include the main class in the base package of the project.
Answered By - João Dias
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.