package com.jiayue.ipfcst.console.service;

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;
import org.springframework.stereotype.Service;

/**
 * 扩展定时器工厂，使定时任务类支持spring注入方法
 *
 * @author zzy
 * @version 2.0
 * @since 2018/10/11 11:24
 */
@Service public class QuartzJobFactory extends SpringBeanJobFactory {

  private final AutowireCapableBeanFactory beanFactory;

  @Autowired public QuartzJobFactory(AutowireCapableBeanFactory beanFactory) {
    this.beanFactory = beanFactory;
  }

  /**
   * 这里覆盖了super的createJobInstance方法，对其创建出来的类再进行autowire。
   */
  @Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {

    Object jobInstance = super.createJobInstance(bundle);

    beanFactory.autowireBean(jobInstance);

    return jobInstance;

  }
}
