Spring quartz Job cannot be dependency injected, and Spring integrated quartz Job tasks cannot be injected
Spring4 integrates the Job task in quartz2.2.3 that cannot be injected using @Autowired
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
©Copyright Sweet Potato Yao August 29, 2017
http://fanshuyao.iteye.com/
1. Problem description:
When using Spring to integrate quartz to implement dynamic tasks, when you want to use a service in the job timing task, you cannot inject it directly by annotating @Component and @Autowired, and the obtained object is Null. Such as the following code:
@Component @PersistJobDataAfterExecution @DisallowConcurrentExecution public class TicketSalePriceLessThanLowestPriceJob implements Job{ @Autowired private XxxService xxxService; }
Second, the solution:
1. Add a custom class (CustomJobFactory), inherit SpringBeanJobFactory, the code is as follows:
import org.quartz.spi.TriggerFiredBundle; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.scheduling.quartz.SpringBeanJobFactory; public class CustomJobFactory extends SpringBeanJobFactory{ @Autowired private AutowireCapableBeanFactory capableBeanFactory; @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { // call the method of the parent class Object jobInstance = super.createJobInstance(bundle); //do the injection capableBeanFactory.autowireBean(jobInstance); return jobInstance; } }
2. Configure the CustomJobFactory in the spring.xml file as follows:
<bean id="customJobFactory" class="cn.imovie.manage.task.job.CustomJobFactory"></bean>
3. Inject the custom CustomJobFactory into org.springframework.scheduling.quartz.SchedulerFactoryBean, as follows:
<property name="jobFactory" ref="customJobFactory"></property>
The complete code is as follows:
<!-- Scheduled task configuration start --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--Optional, QuartzScheduler updates the existing Job when it starts, so that the corresponding records in the qrtz_job_details table do not need to be deleted every time the targetObject is modified --> <property name="overwriteExistingJobs" value="true" /> <!--Required, QuartzScheduler delays startup, QuartzScheduler restarts after application startup--> <property name="startupDelay" value="10" /> <!-- Set autostart--> <property name="autoStartup" value="true" /> <property name="jobFactory" ref="customJobFactory"></property> <property name="applicationContextSchedulerContextKey" value="applicationContextKey" /> <property name="configLocation" value="classpath:spring-quartz.properties" /> </bean> <!-- Scheduled task configuration end -->
4. Then you can use @Autowired to inject the service in the Job task class.
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
©Copyright Sweet Potato Yao August 29, 2017
http://fanshuyao.iteye.com/