校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁(yè) > 知識(shí)庫(kù) > Spring quartz Job依賴(lài)注入使用詳解

Spring quartz Job依賴(lài)注入使用詳解

熱門(mén)標(biāo)簽:如何看懂地圖標(biāo)注點(diǎn) 電話(huà)機(jī)器人需要使用網(wǎng)絡(luò)嗎 電銷(xiāo)機(jī)器人免培訓(xùn) 給地圖標(biāo)注得傭金 潤(rùn)滑油銷(xiāo)售電銷(xiāo)機(jī)器人 外呼系統(tǒng)使用方法 海外圖書(shū)館地圖標(biāo)注點(diǎn) 南通通訊外呼系統(tǒng)產(chǎn)品介紹 自繪地圖標(biāo)注數(shù)據(jù)

Spring quartz Job依賴(lài)注入使用詳解

一、問(wèn)題描述:

使用Spring整合quartz實(shí)現(xiàn)動(dòng)態(tài)任務(wù)時(shí),想在job定時(shí)任務(wù)中使用某個(gè)service時(shí),直接通過(guò)加注解@Component、@Autowired是不能注入的,獲取的對(duì)象為Null。如下面的代碼:

@Component 
@PersistJobDataAfterExecution 
@DisallowConcurrentExecution 
public class TicketSalePriceLessThanLowestPriceJob implements Job{ 
 
  @Autowired 
  private XxxService xxxService; 
 
} 

 二、解決方案:

1、新增一個(gè)自定義類(lèi)(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)用父類(lèi)的方法  
    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> 
 

完整代碼如下:

!-- 定時(shí)任務(wù)配置 start --> 
  bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
    property name="dataSource" ref="dataSource">/property>   
    !--可選,QuartzScheduler 啟動(dòng)時(shí)更新己存在的Job,這樣就不用每次修改targetObject后刪除qrtz_job_details表對(duì)應(yīng)記錄了 -->    
    property name="overwriteExistingJobs" value="true" />    
     !--必須的,QuartzScheduler 延時(shí)啟動(dòng),應(yīng)用啟動(dòng)完后 QuartzScheduler 再啟動(dòng) -->   
    property name="startupDelay" value="10" />   
    !-- 設(shè)置自動(dòng)啟動(dòng) -->   
    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> 
  !-- 定時(shí)任務(wù)配置 end --> 

4、然后就可以在Job任務(wù)類(lèi)使用@Autowired注入service。

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

您可能感興趣的文章:
  • Spring入門(mén)配置和DL依賴(lài)注入實(shí)現(xiàn)圖解
  • spring依賴(lài)注入原理與用法實(shí)例分析
  • 詳解Spring依賴(lài)注入:@Autowired,@Resource和@Inject區(qū)別與實(shí)現(xiàn)原理
  • Spring bean的實(shí)例化和IOC依賴(lài)注入詳解
  • SpringBoot的攔截器中依賴(lài)注入為null的解決方法
  • 淺談Spring IoC容器的依賴(lài)注入原理
  • 理解Spring中的依賴(lài)注入和控制反轉(zhuǎn)
  • Spring依賴(lài)注入的三種方式實(shí)例詳解
  • Spring依賴(lài)注入的三種方式小結(jié)
  • 因Spring AOP導(dǎo)致@Autowired依賴(lài)注入失敗的解決方法
  • Spring依賴(lài)注入的兩種方式(根據(jù)實(shí)例詳解)
  • Spring 依賴(lài)注入的幾種方式詳解
  • Spring 依賴(lài)注入實(shí)現(xiàn)示例

標(biāo)簽:銅川 貸款邀約 廣州 黃石 樂(lè)山 南京 內(nèi)江 大連

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Spring quartz Job依賴(lài)注入使用詳解》,本文關(guān)鍵詞  Spring,quartz,Job,依賴(lài),注入,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Spring quartz Job依賴(lài)注入使用詳解》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Spring quartz Job依賴(lài)注入使用詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 聂拉木县| 成安县| 河西区| 田东县| 渭源县| 密云县| 廉江市| 香格里拉县| 观塘区| 新化县| 通城县| 格尔木市| 鹿邑县| 黄浦区| 都匀市| 高安市| 上蔡县| 抚顺市| 嘉鱼县| 额敏县| 固阳县| 三门峡市| 宜丰县| 仁布县| 中西区| 霞浦县| 峨边| 宁德市| 十堰市| 浦东新区| 清水河县| 兴仁县| 开化县| 会泽县| 盐池县| 微博| 辽中县| 长泰县| 修水县| 荔波县| 开平市|