无法使用 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 (将#修改为@)