如果要在Job中注解调用业务逻辑层Service即下面代码所示
@Autowired
private TestJobService testJobService;
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19package 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配置文件中添加代码如下
复制代码
1
2
3
4
5
6<!-- 定时任务 --> <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobFactory"> <bean class="com.demo.job.MySpringBeanJobFactory"></bean> </property> </bean>
另外需要创建一个MySpringBeanJobFactory类重构createJobInstance方法
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19package 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内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复