Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入

Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入

Spring4整合quartz2.2.3中Job任务使用@Autowired不能注入

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年9月6日

http://www.cnblogs.com/fanshuyao/

 

一、问题描述:

使用Spring整合quartz实现动态任务时,想在job定时任务中使用某个service时,直接通过加注解@Component、@Autowired是不能注入的,获取的对象为Null。如下面的代码:

Java代码  收藏代码
  1. @Component  
  2. @PersistJobDataAfterExecution  
  3. @DisallowConcurrentExecution  
  4. public class TicketSalePriceLessThanLowestPriceJob implements Job{  
  5.   
  6.     @Autowired  
  7.     private XxxService xxxService;  
  8.   
  9. }  

 

 

二、解决方案:

1、新增一个自定义类(CustomJobFactory),继承SpringBeanJobFactory,代码如下:

Java代码  收藏代码
  1. import org.quartz.spi.TriggerFiredBundle;  
  2. import org.springframework.beans.factory.annotation.Autowired;  
  3. import org.springframework.beans.factory.config.AutowireCapableBeanFactory;  
  4. import org.springframework.scheduling.quartz.SpringBeanJobFactory;  
  5.   
  6. public class CustomJobFactory extends SpringBeanJobFactory{  
  7.   
  8.     @Autowired    
  9.     private AutowireCapableBeanFactory capableBeanFactory;    
  10.     
  11.     @Override    
  12.     protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {    
  13.         //调用父类的方法    
  14.         Object jobInstance = super.createJobInstance(bundle);    
  15.         //进行注入    
  16.         capableBeanFactory.autowireBean(jobInstance);    
  17.         return jobInstance;    
  18.     }  
  19.       
  20. }  

 

2、在spring.xml文件配置CustomJobFactory,如下:

Java代码  收藏代码
  1. <bean id="customJobFactory" class="cn.imovie.manage.task.job.CustomJobFactory"></bean>  

 

3、将自定义CustomJobFactory注入到org.springframework.scheduling.quartz.SchedulerFactoryBean,具体如下:

Java代码  收藏代码
  1. <property name="jobFactory" ref="customJobFactory"></property>  

 

完整代码如下:

Java代码  收藏代码
  1. <!-- 定时任务配置 start -->  
  2.     <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    
  3.         <property name="dataSource" ref="dataSource"></property>      
  4.         <!--可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 -->        
  5.         <property name="overwriteExistingJobs" value="true" />        
  6.          <!--必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 -->      
  7.         <property name="startupDelay" value="10" />      
  8.         <!-- 设置自动启动 -->      
  9.         <property name="autoStartup" value="true" />    
  10.         <property name="jobFactory" ref="customJobFactory"></property>  
  11.         <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />  
  12.         <property name="configLocation" value="classpath:spring-quartz.properties" />        
  13.     </bean>  
  14.     <!-- 定时任务配置 end -->  

 

4、然后就可以在Job任务类使用@Autowired注入service。

 

(如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!) 

 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年9月6日

http://www.cnblogs.com/fanshuyao/

 

posted @ 2017-09-06 14:57  蕃薯耀  阅读(3188)  评论(1编辑  收藏  举报