我尝试“ kotlin-spring boot 2-jpa”。我刚刚开始研究spring-boot。 我有模型,存储库和应用程序文件。我使用start.spring.io进行启动。我已经看到了有关网络的示例,但我不需要网络。
编译期间出现错误。为什么呢 如何解决此错误?
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
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
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
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
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
答案 0 :(得分:1)
我们可以创建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
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。