quartzy的spring注入問題

來源:互聯網
上載者:User

標籤:注入   schedule   blog   時間   except   擷取資料   tailf   prot   定時   

<!-- quartz定時器,定時器的類--自己用的類 -->    <bean id="jobDetailFactoryBean" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">        <property name="jobClass" value="com.myk.oe.utils.QuartzTask"></property>    </bean>    <!-- 配置執行時間和規則 -->    <bean id="quartzTask" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">        <property name="jobDetail" ref="jobDetailFactoryBean"></property>        <!-- 5秒一次 -->               <property name="cronExpression" value="0/5 * * * * ?"></property>    </bean>    <!-- 將bean注入到定時器的類 -->    <bean id="jobFactory" class="com.myk.oe.quartz.util.JobFactory"></bean>    <!-- 總管理類 如果將lazy-init=‘false‘那麼容器啟動就會執行發送器 -->     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean" lazy-init="false">        <property name="triggers">            <list>                <ref bean="quartzTask" /><!-- 任務 -->            </list>        </property>        <property name="jobFactory" ref="jobFactory"></property>        <property name="autoStartup" value="true"/><!-- 自動開始 -->    </bean>

  定義好定時任務的相關類,在類中調用dao或者service的時候會報null 指標異常,

在定時任務的類上加註解@DisallowConcurrentExecution就可以注入service和dao了

@DisallowConcurrentExecutionpublic class QuartzTask extends QuartzJobBean{    @Resource(name="appointmentServiceImpl")    private AppointmentService appointmentService;//這是服務類    @Override    protected void executeInternal(JobExecutionContext context)            throws JobExecutionException {    List<Appointment> appointments = appointmentService.getAppointments();//擷取資料        System.err.println(appointments);    }}

 

quartzy的spring注入問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.