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/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM