[英]spring:: lateinit property userRepo has not been initialized
class DataTable(private val param: MultiValueMap<String, String>, private val searchField: Array<String>) {
@Autowired
private val userService: UserService = UserService()
private fun getAllCnt(): Int {
return userService.countAll()
}
@Repository
interface UserRepo: JpaRepository<User, Long> {
@Query(value="SELECT COUNT(*) FROM users", nativeQuery=true)
fun countAll(): Int
}
@Service
class UserService {
@Autowired
private lateinit var userRepo: UserRepo
fun countAll(): Int {
return userRepo.countAll()
}
}
lateinit property userRepo has not been initialized
lateinit 属性 userRepo 尚未初始化
maybe I think not define userRepo.也许我认为不定义 userRepo。 but I don't know how to define userRepo.
但我不知道如何定义 userRepo。
Why used lateinit
?为什么使用
lateinit
? doesn't seem necessary好像没必要
Try fixing it like this尝试像这样修复它
Before change变更前
@Service
class UserService {
@Autowired
private lateinit var userRepo: UserRepo
fun countAll(): Int {
return userRepo.countAll()
}
}
After后
@Service
class UserService(
private val userRepo: UserRepo
) {
fun countAll(): Int {
return userRepo.countAll()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.