lateinit property vkUserRepository has not been initialized. Why?

前端 未结 1 1927
广开言路
广开言路 2021-01-29 03:33

I try \"kotlin - spring boot 2 - jpa\".I just started to study spring-boot. I have model, repository and app files. I`m use start.spring.io for starting.I’ve seen examples with

相关标签:
1条回答
  • 2021-01-29 03:48

    We can create start class-bean

    @Component
    class StartHere {
    @Autowired
    lateinit var vkUserRepository: VkUserRepository
    fun runHere() {
        var users = vkUserRepository.findAll()
        println(users)
    }
    
    //TODO all other program code here
    }
    

    Then in MAIN function we get 'application context' and run our bean-starter

    import org.springframework.boot.autoconfigure.SpringBootApplication
    import org.springframework.boot.runApplication
    import ru.program.vkUsersSkill.init.StartHere
    
    @SpringBootApplication
    class VkUsersSkillApplication
    
    fun main(args: Array<String>) {
    val context = runApplication<VkUsersSkillApplication>(*args)
    val start = context.getBean(StartHere::class.java)
    start.runHere()
    }
    

    All is done. Thanks to @Jonathan Johx.

    0 讨论(0)
提交回复
热议问题