quartz定时任务service注入失败的详细的原因我查了查是这样的:
Job是在quartz的框架中实例化的,service是在spring容器中创建出来的,所以Job实现类不受spring管理,即导致注入失败,所以要想解决这个问题就需要,将他们关联起来。好在Quartz提供了JobFactory接口,就可以自定义实现创建Job的逻辑。就像这样:
public interface JobFactory { Job newJob(TriggerFiredBundle var1, Scheduler var2) throws SchedulerException; }
这个接口所处的位置就是我们在maven引入的Quartz Jar包,是quartz包里面的一个接口,我们通过实现JobFactory接口,在实例化Job以后,在通过ApplicationContext将Job所需要的属性注入即可。
在Spring与Quartz集成时,用到的是SchedulerFactoryBean这个类,这个类是spring-context-support里面的一个类,源码里面有这么一句话(源码很长,我也不会一一解读,很多其实也看不懂,但截取了其中涉及到的一段):
this.scheduler = this.createScheduler(schedulerFactory, this.schedulerName); this.populateSchedulerContext(); if (!this.jobFactorySet && !(this.scheduler instanceof RemoteScheduler)) { this.jobFactory = new AdaptableJobFactory();