简体   繁体   English

spring::lateinit 属性 userRepo 尚未初始化

[英]spring:: lateinit property userRepo has not been initialized

DataTable.kt数据表.kt

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

UserRepo.kt用户报告.kt

@Repository
interface UserRepo: JpaRepository<User, Long> {
    @Query(value="SELECT COUNT(*) FROM users", nativeQuery=true)
    fun countAll(): Int
}

UserService.kt用户服务.kt

@Service
class UserService {
    @Autowired
    private lateinit var userRepo: UserRepo

    fun countAll(): Int {
        return userRepo.countAll()
    }
}

error错误

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.

相关问题 带有 Spring DI 的 Kotlin:lateinit 属性尚未初始化 - Kotlin with Spring DI: lateinit property has not been initialized 从`.properties`文件中检索值| lateinit属性尚未初始化 - Retrieve values from `.properties` file | lateinit property has not been initialized @Autowired 但得到了 lateinit 属性 testBean has not been initialized 错误 - @Autowired but got lateinit property testBean has not been initialized error Kotlin + SpringBoot:DI lateinit属性服务尚未初始化 - Kotlin + SpringBoot : DI lateinit property service has not been initialized kotlin.UninitializedPropertyAccessException:lateinit 属性尚未初始化 - kotlin.UninitializedPropertyAccessException: lateinit property has not been initialized 使用@Autowired 和@Component 时出现lateinit 属性未初始化错误 - Got lateinit property has not been initialized error when using @Autowired and @Component “lateinit 属性<varname>在 Kotlin 上使用带有 SpringBootTest 的 WebTestClient 时尚未初始化”</varname> - “lateinit property <varName> has not been initialized” when using WebTestClient with SpringBootTest on Kotlin UserRepo.findAll()"因为"this.userRepo"在Spring Boot中是null - UserRepo.findAll()" because "this.userRepo" is null in Spring Boot 在Tomcat上部署的Web应用程序中初始化Spring上下文后运行代码 - Run code after Spring context has been initialized in web app deployed on Tomcat Spring thymeleaf TemplateEngine 出现错误 第二次创建pdf时模板引擎已经初始化 - Spring thymeleaf TemplateEngine Getting error Template engine has already been initialized when creating a pdf the second time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM