【问题标题】:xception in thread "Thread-6" kotlin.UninitializedPropertyAccessException: lateinit property has not been initialized线程“Thread-6”kotlin.UninitializedPropertyAccessException 中的异常:lateinit 属性尚未初始化
【发布时间】:2020-04-30 18:28:57
【问题描述】:

大家好,我正在使用 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> {

}

谢谢

【问题讨论】:

  • ProvisioningController 还是(Rest)Controller(或任何其他ComponentBean)?
  • 正常上课
  • 你的类需要由 Spring 管理才能使用自动装配

标签: java spring-boot kotlin autowired kotlin-lateinit


【解决方案1】:

您无需使用lateinit 来自动装配您的存储库

Spring 允许你使用构造函数注入

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

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

【讨论】:

    猜你喜欢
    • 2021-04-25
    • 2017-03-03
    • 1970-01-01
    • 2020-12-31
    • 2021-06-11
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多