简体   繁体   English

Kotlin Spring 使用石英调度程序 class 启动 Autowired lateinit 问题

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

I'm just implements quartz scheduler with kotlin and spring boot.我只是用 kotlin 和 spring 启动实现了石英调度程序。

kotlin.UninitializedPropertyAccessException: lateinit property userController has not been initialized kotlin.UninitializedPropertyAccessException:lateinit 属性 userController 尚未初始化

I know userController Object is not created with @autowired basically in kotlin lateinit create object lazy so i d'not have idea to fix this issue.我知道 userController Object 基本上不是在 kotlin 中使用 @autowired 创建的,所以我不知道如何解决这个问题。 any possible way to create object in kotlin with eagerly to fix this issue任何可能的方法在 kotlin 中创建 object 并急切地解决此问题


I'm Getting the following error:我收到以下错误:

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 

Code:代码:


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

Have you implemented a custom SpringBeanJobFactory that would allow dependency 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
}

} }

The controller can also be passed on via the constructor: 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 can also be omitted. @Autowired 也可以省略。 For more information about Kotlin and Spring Boot:有关 Kotlin 和 Spring 引导的更多信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM