本文介绍了lateinit属性vkUserRepository尚未初始化.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试"kotlin-spring boot 2-jpa".我刚刚开始研究spring-boot.我有模型,存储库和应用程序文件.我使用start.spring.io进行启动.我已经看到了有关网络的示例,但我不需要网络.

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 web, but I don’t need a web.

我在编译过程中出错.为什么 ?如何解决此错误?

I have error during compilation. Why ?How can I fix this error?

2018-12-21 13:26:02.732  INFO 28188 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
2018-12-21 13:26:02.923  INFO 28188 --- [           main] o.h.e.j.e.i.LobCreatorBuilderImpl        : HHH000421: Disabling contextual LOB creation as hibernate.jdbc.lob.non_contextual_creation is true
2018-12-21 13:26:02.929  INFO 28188 --- [           main] org.hibernate.type.BasicTypeRegistry     : HHH000270: Type registration [java.util.UUID] overrides previous :         org.hibernate.type.UUIDBinaryType@396ef8b2
2018-12-21 13:26:03.335  INFO 28188 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-12-21 13:26:04.150  INFO 28188 --- [           main] r.k.v.VkUsersSkillApplicationKt          : Started VkUsersSkillApplicationKt in 4.348 seconds (JVM running for 4.905)
Exception in thread "main" kotlin.UninitializedPropertyAccessException: lateinit property vkUserRepository has not been initialized
  at     ru.program.vkUsersSkill.VkUsersSkillApplicationKt.main(VkUsersSkillApplication.kt:17)

我的程序:

VkUser.kt

VkUser.kt

import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.Id
import javax.persistence.Table

@Entity
@Table (name = "vk_users")
data class VkUser(
    @Id
    @Column(name = "user_id")
    var userId: Long = 0L,

    @Column(name = "access_token")
    var accessToken: String = "",

    @Column(name = "alias")
    var alias: String = "",

    @Column(name = "login")
    var login: String = "",

    @Column(name = "password")
    var password: String = ""
)

VkUserRepository.kt

VkUserRepository.kt

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import ru.program.vkUsersSkill.models.VkUser

@Repository
interface VkUserRepository: JpaRepository<VkUser, Long>

VkUsersSkillApplication.kt

VkUsersSkillApplication.kt

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import ru.program.vkUsersSkill.repositories.VkUserRepository

@Autowired
lateinit var vkUserRepository: VkUserRepository

@SpringBootApplication
class VkUsersSkillApplication

fun main(args: Array<String>) {
  runApplication<VkUsersSkillApplication>(*args)
  vkUserRepository.findAll()
}

application.properties

application.properties

spring.jpa.database-platform =org.hibernate.dialect.PostgreSQL95Dialect
spring.datasource.url=jdbc:postgresql://localhost:5432/vkadmin
spring.datasource.username=vkadmin
spring.datasource.password=pass
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

推荐答案

我们可以创建start class-bean

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
}

然后在MAIN函数中,我们获得应用程序上下文"并运行我们的bean-starter

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

全部完成.感谢@Jonathan Johx.

All is done.Thanks to @Jonathan Johx.

这篇关于lateinit属性vkUserRepository尚未初始化.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 11:20