zoukankan      html  css  js  c++  java
  • Spring使用Quartz定时调度Job无法Autowired注入Service的解决方案

     1)自定义JobFactory,通过spring的AutowireCapableBeanFactory进行注入,例如:

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

    {

        @Autowired

        private AutowireCapableBeanFactory beanFactory;

        /**

         * 这里覆盖了super的createJobInstance方法,对其创建出来的类再进行autowire。

         */

        @Override

        protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {

            Object jobInstance = super.createJobInstance(bundle);

            beanFactory.autowireBean(jobInstance);

            return jobInstance;

        }

    }

    2)定义上述类之后,需要在定义触发器,引用org.springframework.scheduling.quartz.SpringBeanJobFactory的地方,配置property,例如(见红色部分):

    <bean name="quartzScheduler" lazy-init="false"
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="dataSource" ref="ds" />
    <property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
    <property name="configLocation" value="classpath:quartz.properties" />
    <property name="triggers">
    <list>
    <ref bean="trigger" />
    </list>
    </property>
    <property name="jobFactory">
                <bean class="com.xxx.MyJobFactory" />
            </property>
    </bean>

  • 相关阅读:
    授权管理-LDAP-介绍与环境搭建
    Gerrit系统框架介绍
    代码托管-gerrit-介绍与环境搭建
    springMVC
    spring,springMVC,mybatis项目添加maven后报500错
    SSM项目引入文件失败
    spring与mybatis整合
    幂等问题本质和业务场景的解决方案
    spring boot logback无感配置
    sentinel与hystrix对比
  • 原文地址:https://www.cnblogs.com/duanxianyouyang/p/7967877.html
Copyright © 2011-2022 走看看