我只是用 kotlin 和 spring boot 实现了 quartz 调度器。
kotlin.UninitializedPropertyAccessException: lateinit 属性 userController 尚未初始化
我知道 userController 对象基本上不是在 kotlin lateinit create object lazy 中使用 @autowired 创建的,所以我不知道如何解决这个问题。在 kotlin 中创建对象的任何可能方法,急切地解决这个问题
我收到以下错误:
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
最佳答案
您是否实现了允许依赖项 Autowiring 的自定义 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
}
关于spring - quartz 调度程序类的 Kotlin Spring Boot Autowired lateinit 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61159656/