package link.at17.mid.tushare.task;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

/**
 * Job 工厂，使得 Job 实例能获取 Spring 管理的 Bean
 * <p>
 * 如某 UpdatePlan implements Job, 在其 execute 方法中需要调用 updatePlanService, 则必须要实例能获取 updatePlanService
 */
@Component
public class AutowireCapableJobFactory extends AdaptableJobFactory {
    
	@Autowired
	private AutowireCapableBeanFactory capableBeanFactory;

	protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
		Object jobInstance = super.createJobInstance(bundle);
		capableBeanFactory.autowireBean(jobInstance); // 自动注入 Spring Bean
		return jobInstance;
	}
}