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>

  • 相关阅读:
    一、计算机网络概述
    一些早期的sftp在openssh升级到 openssh7可能闪断解法
    ssh: error while loading shared libraries: libcrypto.so.1.0.0
    PHP Warning: imagettftext(): Problem loading glyph in
    compile pcre on vs2008
    《祝总骧312经络锻炼法》
    神秘的经络
    益嗅上清汤
    鼻病 《仁术便览》
    鼻(附嚏)《医述》
  • 原文地址:https://www.cnblogs.com/duanxianyouyang/p/7967877.html
Copyright © 2011-2022 走看看