日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring3 M2 quartz-2.1.7 解决bean不能注入问题

發(fā)布時(shí)間:2025/5/22 javascript 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring3 M2 quartz-2.1.7 解决bean不能注入问题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我們要達(dá)到這樣的效果

public class CancelUnpaidOrderTask implements Job {@Autowiredprivate AppOrderService orderService; @Override public void execute(JobExecutionContext ctx) throws JobExecutionException { ... }

但是Job對(duì)象的實(shí)例化過程是在Quartz中進(jìn)行的,AppOrderService是在Spring容器當(dāng)中的,那么如何將他們關(guān)聯(lián)到一起呢。好在Quartz提供了JobFactory接口,讓我們可以自定義實(shí)現(xiàn)創(chuàng)建Job的邏輯。

public interface JobFactory {
Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException;
}

那么我們通過實(shí)現(xiàn)JobFactory 接口,在實(shí)例化Job以后,在通過ApplicationContext 將Job所需要的屬性注入即可

在Spring與Quartz集成時(shí) 用到的是org.springframework.scheduling.quartz.SchedulerFactoryBean這個(gè)類。源碼如下,我們只看最關(guān)鍵的地方。

// Get Scheduler instance from SchedulerFactory.try {this.scheduler = createScheduler(schedulerFactory, this.schedulerName); populateSchedulerContext(); if (!this.jobFactorySet && !(this.scheduler instanceof RemoteScheduler)) { // Use AdaptableJobFactory as default for a local Scheduler, unless when // explicitly given a null value through the "jobFactory" bean property. this.jobFactory = new AdaptableJobFactory(); } if (this.jobFactory != null) { if (this.jobFactory instanceof SchedulerContextAware) { ((SchedulerContextAware) this.jobFactory).setSchedulerContext(this.scheduler.getContext()); } this.scheduler.setJobFactory(this.jobFactory); } }

其中紅色標(biāo)記的是重點(diǎn),如果我們不指定jobFactory,那么Spring就使用AdaptableJobFactory。我們?cè)趤砜匆幌逻@個(gè)類的實(shí)現(xiàn)

package org.springframework.scheduling.quartz;import java.lang.reflect.Method;import org.quartz.Job; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.spi.JobFactory; import org.quartz.spi.TriggerFiredBundle; import org.springframework.util.ReflectionUtils; public class AdaptableJobFactory implements JobFactory { public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException { return newJob(bundle); } public Job newJob(TriggerFiredBundle bundle) throws SchedulerException { try { Object jobObject = createJobInstance(bundle); return adaptJob(jobObject); } catch (Exception ex) { throw new SchedulerException("Job instantiation failed", ex); } } protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { // Reflectively adapting to differences between Quartz 1.x and Quartz 2.0... Method getJobDetail = bundle.getClass().getMethod("getJobDetail"); Object jobDetail = ReflectionUtils.invokeMethod(getJobDetail, bundle); Method getJobClass = jobDetail.getClass().getMethod("getJobClass"); Class jobClass = (Class) ReflectionUtils.invokeMethod(getJobClass, jobDetail); return jobClass.newInstance(); } protected Job adaptJob(Object jobObject) throws Exception { if (jobObject instanceof Job) { return (Job) jobObject; } else if (jobObject instanceof Runnable) { return new DelegatingJob((Runnable) jobObject); } else { throw new IllegalArgumentException("Unable to execute job class [" + jobObject.getClass().getName() + "]: only [org.quartz.Job] and [java.lang.Runnable] supported."); } } }

其他的我們都不管,我們就看紅色的地方,這里是創(chuàng)建了一個(gè)Job,那我們就在這里去給Job的屬性進(jìn)行注入就可以了,讓我們寫一個(gè)類繼承它,然后復(fù)寫這個(gè)方法進(jìn)行對(duì)Job的注入。

按 Ctrl+C 復(fù)制代碼 按 Ctrl+C 復(fù)制代碼

接下來把他配置到Spring當(dāng)中去

<bean id="jobFactory" class="com.gary.operation.jobdemo.demo1.MyJobFactory"></bean>

然后在把org.springframework.scheduling.quartz.SchedulerFactoryBean的jobFactory設(shè)置成我們自己的。

<bean name="MyScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <!-- 其他屬性省略 -->   <property name="jobFactory" ref="jobFactory"></property> </bean>

這樣就完成了Spring對(duì)Job的注入功能,其實(shí)很簡單,原理就是在我們擴(kuò)展JobFactory創(chuàng)建job的方法,在創(chuàng)建完Job以后進(jìn)行屬性注入。

轉(zhuǎn)載于:https://www.cnblogs.com/zhaofeng555/p/5783281.html

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Spring3 M2 quartz-2.1.7 解决bean不能注入问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。