Quartz的job中注入的services接口为空的解决办法
Posted Quest
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Quartz的job中注入的services接口为空的解决办法相关的知识,希望对你有一定的参考价值。
自己重新定义一个类继承AdaptableJobFactory类
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; } }
在sprig配置文件中配置
<bean id="jobFactory" class="com.ka.common.quartz.JobFactory"></bean> <!-- 调度器 --> <bean name="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobFactory" ref="jobFactory"></property> <!-- 通过applicationContextSchedulerContextKey属性配置spring上下文 --> <property name="applicationContextSchedulerContextKey"> <value>applicationContext</value> </property> </bean>
在job中使用
public class QuartzTaskMeetingNoticeHandler implements Job { protected Logger log = LoggerFactory.getLogger(getClass()); @Resource(name = "AdminJournalServices") protected IAdminJournalServices mIAdminJournalServices; @Resource(name = "AdminAccountServices") protected IAdminAccountServices mIAdminAccountServices; @Override public void execute(JobExecutionContext context) throws JobExecutionException { log.debug("-------------------获取调度任务 start--------------------"); } }
引用参考:http://blog.csdn.net/irabbit0708/article/details/52023437
以上是关于Quartz的job中注入的services接口为空的解决办法的主要内容,如果未能解决你的问题,请参考以下文章
java 框架-spring 整合 quartz 框架 service层 注入不了job 类
微信公众号的SpringBoot+Quartz的定时任务Demo
Spring quartz Job不能依赖注入,Spring整合quartz Job任务不能注入