Quartz 和 Spring 启动问题

Issue with Quartz and Spring Boot

所以我遇到了技术挑战,需要帮助。

一个大型项目正在使用 Quartz 调度程序将作业安排到每晚 9 点运行。
已安排的作业需要从 属性 文件中读取值,使用自动装配等获取一些 bean。

当我使用@Autowired 和@Value 注解时,我发现这些值是空的。

问题是 Quartz 在 spring 容器之外使用 newJob() 创建 JobDetail 对象。从下面的代码可以看出。

JobKey jobKey = new JobKey("NightJob", "9-PM Job");
JobDetail jobDetail = newJob(NightJob.class).withIdentity(jobKey)
                     .usingJobData("Job-Id", "1")
                     .build();

包装 NightJobjobDetail 对象因此无法使用 spring.

访问 属性 文件或 bean

这是我的NightJobclass

public class NightJob implements Job{

    //@Value to read from property file; here
    //@Autowired to use some beans; here

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException{
    }
}

我浏览了 Stack Overflow 并列出了几个解决方案。我还通读了评论并列出了最热门的反评论。

Suggestion 1: Get rid of Quartz and use Spring Batch due to its good integration with Spring Boot

Counter argument 1: Spring Batch is overkill for simple tasks. Use @Scheduled

Suggestion 2: Use @Scheduled annotations and cron expressions provided by spring

Counter argument 2: Your application will not be future ready if you remove Quartz. Complex scheduling may be required in the future

Suggestion 3 : Use the spring interface ApplicationContextAware.

Counter argument 3: Lots of additional code. Defeats the simple and easy concept of Spring boot

在 Spring 引导中是否有更简单的方法来访问 属性 文件值和实现 Quartz 作业的 class 中的自动装配对象(在这种情况下, NightJob class)

如评论中所写,Spring 通过提供 setter 方法支持将 bean 注入到 Quartz 作业中:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-quartz.html