java - 线程 "Thread-6"kotlin.UninitializedPropertyAccessException : lateinit property has not been initialized 中的 xception

标签 java spring-boot kotlin autowired kotlin-lateinit

大家好,我正在使用 Spring Boot 开发一个应用程序,尽管我在生成以下错误的线程中初始化 JPA 存储库时遇到一些问题

Exception in thread "Thread-6" kotlin.UninitializedPropertyAccessException: lateinit property contactTypeAudioPathRepository has not been initialized
    at com.sopristec.sa_tas.controllers.ProvisioningController$createAudioWithAsync$executeP$1.run(ProvisioningController.kt:96)
    at java.lang.Thread.run(Thread.java:748)

我使用 @Autowired 来启动它,这就是该函数的样子

@Autowired
private  lateinit var contactTypeAudioPathRepository: ContactTypeAudioPathRepository


fun createAudioWithAsync(menuChoice: String, presentation: String) {

        val executeP = Thread{
            val message = "Press $menuChoice to save this phone number as $presentation. Or " +
                    "Press 9 to save as work number"


            val commandVoice = mutableListOf<String>("python","pythonScript/main.py",message);

            val buildAudio = ProcessBuilder(commandVoice).start()

            val getPath = buildAudio.inputStream.bufferedReader().use { it.readText() }

            if(getPath.isEmpty()){
                println("A python script failed")
            }
            println(getPath.replace("\n","").replace("att_sas_pyttsx3/",""))

            val fileName = getPath.replace("\n","")
            contactTypeAudioPathRepository.save(ContactTypeAudioPath(0,fileName))

           // Genera error --> contactTypeAudioPathRepository.add("mediaLink").subscribe()


        }.start()
//        executeP.start();
        //contactTypeAudioPathRepository.add("mediaLink").subscribe()

    }

这是存储库,正如您所看到的,是 JPA 的 CrudRepository

@Repository
interface ContactTypeAudioPathRepository : CrudRepository<ContactTypeAudioPath,Int> {

}

谢谢

最佳答案

您不需要使用lateinit来 Autowiring 您的存储库

Spring 允许您使用构造函数注入(inject)

@Component
class ProvisioningController(private val repository:ContactTypeAudioPathRepository) {

   fun createAudioWithAsync(menuChoice: String, presentation: String) {
       ....
   }
}

关于java - 线程 "Thread-6"kotlin.UninitializedPropertyAccessException : lateinit property has not been initialized 中的 xception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61530299/

相关文章:

java - Java中引导方法如何注册到常量池中?

java - 将多个参数传递给 Pig Filter UDF

java - 接收Java : Sleep before OnNext (Sleep before emitting from Observable)

kotlin - 六边形架构和 DDD

android - 同时运行 10 个 kotlin 异步协程

java - 如何在JavaFX应用程序中从Excel读取数据?

java - Firestore 迭代文档以查找空字段

java - Junit5 @ParameterizedTest 如何将数组作为参数之一传递

java - Spring-boot 应用程序在执行器端口上抛出错误

java - Spring 启动 1.5.10.RELEASE : REST service keeps geting 404