五)Spring + Quartz 复杂业务的两个问题:获取Spring上下文 和 自动注入服务类

配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:task="http://www.springframework.org/schema/task"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <context:component-scan base-package="cn.zno" />
    <task:executor id="threadPoolTaskExecutor" pool-size="1" />
    
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobFactory">
            <bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
        </property>
        <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
        <property name="startupDelay" value="0" />
        <property name="overwriteExistingJobs" value="true" />
        <property name="exposeSchedulerInRepository" value="true" />
        <property name="taskExecutor" ref="threadPoolTaskExecutor" />
        <property name="triggers">
            <list>
                <ref bean="cronTrigger_1" />
            </list>
        </property>
    </bean>

    <bean id="cronTrigger_1"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="jobDetail_1" />
        <property name="cronExpression" value="* * * * * ?" />
    </bean>
    <bean id="jobDetail_1"
        class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
        <property name="jobClass" value="cn.zno.job.Breathe" />
    </bean>
    <bean id="breath" class="cn.zno.job.Breathe" />
</beans>

为什么要配置这个

<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />

因为可以通过这个key取到Spring上下文。

配置1存在的问题:不能自动注入。

package cn.zno.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;

import cn.zno.service.AService;

public class Breathe implements Job {
    
//    @Autowired
//    private AService aService;

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        try {
//            aService.Show();
            ApplicationContext ctx = (ApplicationContext)context.getScheduler().getContext().get("applicationContextKey");
            AService aService = ctx.getBean(AService.class);
            aService.Show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
    }


}

配置一改:解决自动注入问题

change

        <property name="jobFactory">
            <bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory" />
        </property>

to

        <property name="jobFactory">
            <bean class="cn.zno.common.SpringBeanJobFactory" />
        </property>

cn.zno.common.SpringBeanJobFactory

package cn.zno.common;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringBeanJobFactory extends
        org.springframework.scheduling.quartz.SpringBeanJobFactory implements
        ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.applicationContext = applicationContext;

    }

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle)throws Exception {
        Object jobInstance = super.createJobInstance(bundle);
        applicationContext.getAutowireCapableBeanFactory().autowireBean(jobInstance);
        return jobInstance;
    }
}

另一种实现方式为:

package cn.zno.common;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;

public class SpringBeanJobFactory extends
        org.springframework.scheduling.quartz.SpringBeanJobFactory {

    @Autowired
    private AutowireCapableBeanFactory autowireCapableBeanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle)throws Exception {
        Object jobInstance = super.createJobInstance(bundle);
        autowireCapableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}
原文地址:https://www.cnblogs.com/zno2/p/4891251.html