亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Spring中將bean引用注入Quartz作業?

在Spring中將bean引用注入Quartz作業?

慕姐8265434 2019-08-12 10:03:23
在Spring中將bean引用注入Quartz作業?我設法在Spring中使用JobStoreTX持久存儲來配置和調度Quartz作業。我不使用Spring的Quartz作業,因為我需要在運行時動態調度它們,并且我發現Spring與Quartz集成的所有示例都是對Spring配置文件中的shcedules進行硬編碼...無論如何,這里是如何我安排工作:JobDetail emailJob = JobBuilder.newJob(EMailJob.class).withIdentity("someJobKey", "immediateEmailsGroup").storeDurably().build();SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger() .withIdentity("someTriggerKey", "immediateEmailsGroup").startAt(fireTime).build();// pass initialization parameters into the jobemailJob.getJobDataMap().put(NotificationConstants.MESSAGE_PARAMETERS_KEY,       messageParameters);emailJob.getJobDataMap().put(NotificationConstants.RECIPIENT_KEY, recipient);if (!scheduler.checkExists(jobKey) && scheduler.getTrigger(triggerKey) != null)     {                                       // schedule the job to runDate scheduleTime1 = scheduler.scheduleJob(emailJob, trigger);}EMailJob是一個簡單的工作,它使用Spring的JavaMailSenderImpl類發送電子郵件。public class EMailJob implements Job {@Autowiredprivate JavaMailSenderImpl mailSenderImpl;     public EMailJob() {     }     public void execute(JobExecutionContext context)        throws JobExecutionException {    ....     try {         mailSenderImpl.send(mimeMessage);     } catch (MessagingException e) {         ....         throw new JobExecutionException("EMailJob failed: " +  jobKey.getName(), e);     }     logger.info("EMailJob finished OK");}問題是我需要在我的EMailJob類中獲得對該類的實例(JavaMailSenderImpl)的引用。當我嘗試像這樣注入它:@Autowiredprivate JavaMailSenderImpl mailSenderImpl;它沒有被注入 - 引用是NULL。我假設發生了這種情況,因為實例化EMailJob類不是Spring,但是Quartz和Quartz對依賴注入一無所知......那么,是否有某種方法可以強制進行這種注射?謝謝!
查看完整描述

3 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

我只是SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);作為我的Job.execute(JobExecutionContext context)方法的第一行。


查看完整回答
反對 回復 2019-08-12
  • 3 回答
  • 0 關注
  • 811 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號