package com.qqchen.deploy.backend.schedule.config;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

/**
 * Quartz Job工厂
 * 支持Spring依赖注入
 *
 * @author qichen
 */
@Component
public class QuartzJobFactory extends SpringBeanJobFactory {

    private final AutowireCapableBeanFactory beanFactory;

    public QuartzJobFactory(ApplicationContext applicationContext) {
        this.beanFactory = applicationContext.getAutowireCapableBeanFactory();
    }

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        // 调用父类创建Job实例
        Object jobInstance = super.createJobInstance(bundle);
        // 注入Spring依赖
        beanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}

