生活随笔
收集整理的這篇文章主要介紹了
解决quartz的job无法注入spring对象
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一般情況下,quartz的job中使用autowired注解注入的對象為空,這時候我們就要使用spring-quartz提供的AdaptableJobFactory類。
自定義一個類:
[java]?view plaincopy
public?class?JobFactory?extends?AdaptableJobFactory?{?? ?????? ????@Autowired?? ????private?AutowireCapableBeanFactory?capableBeanFactory;?? ?? ????@Override?? ????protected?Object?createJobInstance(TriggerFiredBundle?bundle)?throws?Exception?{?? ?????????? ????????Object?jobInstance?=?super.createJobInstance(bundle);?? ?????????? ????????capableBeanFactory.autowireBean(jobInstance);?? ????????return?jobInstance;?? ????}?? ?????? }??
然后在spring中配置:
[html]?view plaincopy
?? ????<bean?id="jobFactory"?class="com.xx.job.JobFactory"></bean>?? ????<bean?id="schedulerFactoryBean"?class="org.springframework.scheduling.quartz.SchedulerFactoryBean">?? ????????<property?name="jobFactory"?ref="jobFactory"></property>?? ????</bean>??
這時候,我們在定義類繼承job的時候,就可以使用autowired注入service對象了:
[java]?view plaincopy
public?class?Test1Job?implements?Job?{?? ????public?final?Logger?log?=?Logger.getLogger(this.getClass());?? ?????? ????@Autowired?? ????private?JobTaskService?jobTaskService;?? ?????? ????public?void?execute(JobExecutionContext?context)?throws?JobExecutionException?{?? ?????????? ????????Date?nextProcessTime?=?context.getNextFireTime();?? ????????ScheduleJob?job?=?(ScheduleJob)?context.getJobDetail().getJobDataMap().get("scheduleJob");?? ????????job.setNextProcessTime(nextProcessTime);?? ????????jobTaskService.updateTaskByJobName(job);?? ?????????? ?????????? ????????System.out.println("22222222222222222222:");?? ????}?? }??
總結
以上是生活随笔為你收集整理的解决quartz的job无法注入spring对象的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。