概述
如果要在Job中注解调用业务逻辑层Service即下面代码所示
@Autowired
private TestJobService testJobService;
package com.demo.job;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import com.demo.service.job.TestJobService;
public class QuartzJobFactory implements Job {
private final Logger log = Logger.getLogger(QuartzJobFactory.class);
@Autowired
private TestJobService testJobService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
log.info("----->>任务开始<<-----");
ScheduleJob scheduleJob = (ScheduleJob) context.getMergedJobDataMap().get("scheduleJob");
System.out.println("任务名称 = [" + scheduleJob.getJobName() + "]t任务组 = [" + scheduleJob.getJobGroup() + "]");
testJobService.backupCust();
}
}
需要在spring配置文件中添加代码如下
<!-- 定时任务 -->
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobFactory">
<bean class="com.demo.job.MySpringBeanJobFactory"></bean>
</property>
</bean>
另外需要创建一个MySpringBeanJobFactory类重构createJobInstance方法
package com.demo.job;
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 MySpringBeanJobFactory extends SpringBeanJobFactory {
@Autowired
private AutowireCapableBeanFactory beanFactory;
/**
* 这里我们覆盖了super的createJobInstance方法,对其创建出来的类再进行autowire。
*/
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
beanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
最后
以上就是友好背包为你收集整理的quartz注解调用service的全部内容,希望文章能够帮你解决quartz注解调用service所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复