Spring quartz Job依賴注入使用詳解
Spring quartz Job依賴注入使用詳解
一、問題描述:
使用Spring整合quartz實(shí)現(xiàn)動態(tài)任務(wù)時,想在job定時任務(wù)中使用某個service時,直接通過加注解@Component、@Autowired是不能注入的,獲取的對象為Null。如下面的代碼:
@Component @PersistJobDataAfterExecution @DisallowConcurrentExecution public class TicketSalePriceLessThanLowestPriceJob implements Job{ @Autowired private XxxService xxxService; }
二、解決方案:
1、新增一個自定義類(CustomJobFactory),繼承SpringBeanJobFactory,代碼如下:
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 { //調(diào)用父類的方法 Object jobInstance = super.createJobInstance(bundle); //進(jìn)行注入 capableBeanFactory.autowireBean(jobInstance); return jobInstance; } }
2、在spring.xml文件配置CustomJobFactory,如下:
<bean id="customJobFactory" class="cn.imovie.manage.task.job.CustomJobFactory"></bean>
3、將自定義CustomJobFactory注入到org.springframework.scheduling.quartz.SchedulerFactoryBean,具體如下:
<property name="jobFactory" ref="customJobFactory"></property>
完整代碼如下:
<!-- 定時任務(wù)配置 start --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--可選,QuartzScheduler 啟動時更新己存在的Job,這樣就不用每次修改targetObject后刪除qrtz_job_details表對應(yīng)記錄了 --> <property name="overwriteExistingJobs" value="true" /> <!--必須的,QuartzScheduler 延時啟動,應(yīng)用啟動完后 QuartzScheduler 再啟動 --> <property name="startupDelay" value="10" /> <!-- 設(shè)置自動啟動 --> <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> <!-- 定時任務(wù)配置 end -->
4、然后就可以在Job任務(wù)類使用@Autowired注入service。
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Spring入門配置和DL依賴注入實(shí)現(xiàn)圖解
- spring依賴注入原理與用法實(shí)例分析
- 詳解Spring依賴注入:@Autowired,@Resource和@Inject區(qū)別與實(shí)現(xiàn)原理
- Spring bean的實(shí)例化和IOC依賴注入詳解
- SpringBoot的攔截器中依賴注入為null的解決方法
- 淺談Spring IoC容器的依賴注入原理
- 理解Spring中的依賴注入和控制反轉(zhuǎn)
- Spring依賴注入的三種方式實(shí)例詳解
- Spring依賴注入的三種方式小結(jié)
- 因Spring AOP導(dǎo)致@Autowired依賴注入失敗的解決方法
- Spring依賴注入的兩種方式(根據(jù)實(shí)例詳解)
- Spring 依賴注入的幾種方式詳解
- Spring 依賴注入實(shí)現(xiàn)示例
相關(guān)文章
jsp實(shí)現(xiàn)簡單用戶7天內(nèi)免登錄
這篇文章主要為大家詳細(xì)介紹了jsp實(shí)現(xiàn)簡單用戶7天內(nèi)免登錄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-02-02JAVA/JSP學(xué)習(xí)系列之一(JDK安裝)
JAVA/JSP學(xué)習(xí)系列之一(JDK安裝)...2006-10-10JSP+jquery使用ajax方式調(diào)用json的實(shí)現(xiàn)方法
這篇文章主要介紹了JSP+jquery使用ajax方式調(diào)用json的實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了前臺Ajax調(diào)用及后臺JSP的處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11Jsp+Servlet實(shí)現(xiàn)簡單登錄注冊查詢
這篇文章主要介紹了Jsp+Servlet實(shí)現(xiàn)簡單登錄注冊查詢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09Spring 整合 Hibernate 時啟用二級緩存實(shí)例詳解
這篇文章主要介紹了Spring 整合 Hibernate 時啟用二級緩存實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02Java 創(chuàng)建cookie和刪除cookie
java下創(chuàng)建cookie的代碼,包括了創(chuàng)建跟刪除。2009-04-04