自己重新定义一个类继承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