簡體   English   中英

Kotlin Spring 使用石英調度程序 class 啟動 Autowired lateinit 問題

[英]Kotlin Spring Boot Autowired lateinit issue with quartz scheduler class

我只是用 kotlin 和 spring 啟動實現了石英調度程序。

kotlin.UninitializedPropertyAccessException:lateinit 屬性 userController 尚未初始化

我知道 userController Object 基本上不是在 kotlin 中使用 @autowired 創建的,所以我不知道如何解決這個問題。 任何可能的方法在 kotlin 中創建 object 並急切地解決此問題


我收到以下錯誤:

Full Error Log : For reference


21:02:00.037 [DatabaseScheduler_Worker-1] ERROR org.quartz.core.JobRunShell - Job commentJobGroup.commentJob threw an unhandled Exception: 
kotlin.UninitializedPropertyAccessException: lateinit property userController has not been initialized
    at com.lovevirus.kotlinQuartzScheduler.scheduler.Jobs.CommentJob.getUserController(CommentJob.kt:17)
    at com.lovevirus.kotlinQuartzScheduler.scheduler.Jobs.CommentJob.execute(CommentJob.kt:21)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
21:02:00.038 [DatabaseScheduler_Worker-1] ERROR org.quartz.core.ErrorLogger - Job (commentJobGroup.commentJob threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception.
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: kotlin.UninitializedPropertyAccessException: lateinit property userController has not been initialized
    at com.lovevirus.kotlinQuartzScheduler.scheduler.Jobs.CommentJob.getUserController(CommentJob.kt:17)
    at com.lovevirus.kotlinQuartzScheduler.scheduler.Jobs.CommentJob.execute(CommentJob.kt:21)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    ... 1 common frames omitted 

代碼:


import org.quartz.Job
import org.quartz.JobExecutionContext
import org.springframework.stereotype.Component
import com.lovevirus.kotlinQuartzScheduler.controller.UserController;
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired


@Component
class CommentJob : Job {
    private val logger: Logger = LoggerFactory.getLogger(CommentJob::class.java)
    @Autowired
    lateinit var userController : UserController;

    override fun execute(p0: JobExecutionContext?) {
      logger.debug("Inside Comment Job");
        userController.getUser();
       logger.debug("End Comment Job");
    }
}



Thanks in advance

您是否實現了允許依賴自動裝配的自定義SpringBeanJobFactory

class AutowiringSpringBeanJobFactory : SpringBeanJobFactory(), ApplicationContextAware {

@Transient
private var beanFactory: AutowireCapableBeanFactory? = null

override fun setApplicationContext(applicationContext: ApplicationContext) {
    beanFactory = applicationContext.autowireCapableBeanFactory
}

override fun createJobInstance(bundle: TriggerFiredBundle): Any {
    val job = super.createJobInstance(bundle)
    beanFactory!!.autowireBean(job)
    return job
}

}

controller 也可以通過構造函數傳遞:

@Component
class CommentJob(@Autowired userController: UserController): Job {

    private val logger: Logger = LoggerFactory.getLogger(CommentJob::class.java)
   
    override fun execute(p0: JobExecutionContext?) {
        logger.debug("Inside Comment Job");
        userController.getUser();
        logger.debug("End Comment Job");
    }
}

@Autowired 也可以省略。 有關 Kotlin 和 Spring 引導的更多信息:

https://www.baeldung.com/spring-boot-kotlin

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM