如何解决无法使用 picocli 和 micronaut 连接数据库
我使用 picocli 连接数据库并构建本机图像。当我执行由 picocli 本地构建的图像时,我使用 micronaut 连接到 postgres 获取一些数据。但是我在使用 entityManager 时遇到了问题。当我使用@Inject 使用实体管理器时,它总是抛出异常
kotlin.UninitializedPropertyAccessException: lateinit property entityManager has not been initialized
代码如下
@Singleton
class StudentRepository {
@Inject
lateinit var entityManager: EntityManager
@ReadOnly
fun findAll(): List<Student?>? {
var qlString = "SELECT * FROM Student as g"
val query: TypedQuery<Student> = entityManager.createQuery(qlString,Student::class.java)
return query.getResultList()
}
}
然后我调用的服务看起来像
@CommandLine.Command(name = "studentService",description = arrayOf("Student service"),mixinStandardHelpOptions = true)
@Singleton
class StudentService(): Runnable {
@Inject
lateinit var studentRepository: StudentRepository
override fun run() {
getAllStudent()
}
fun getAllStudent() {
println("Count student ${studentRepository.findAll().size}")
}
}
我的申请
@picocli.CommandLine.Command(
name = "cli",description = ["Cli"],version = ["Cli"],mixinStandardHelpOptions = true,subcommands = [StudentService::class]
)
class MyApplication : Runnable {
override fun run() {
println("Begin start")
}
}
fun main(args: Array<String>) {
PicocliRunner.run(MyApplication::class.java,*args)
}
但是如果我使用 http 客户端,它就可以工作。 https://dev.to/stack-labs/cli-applications-with-micronaut-and-picocli-4mc8 我不知道为什么不能使用 entityManager 。如何使用 picocli 连接数据库
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。