spring3.0定時任務 (一) 簡單樣本

來源:互聯網
上載者:User

        在項目中經常用到的spring的一個功能就是定時任務,它可以自動監視時間,到點就執行,給程式帶來了很大的方便,很多地方都會需要這種功能,比如做資料備份、同步等操作。最近一直比較忙,主要是比較懶,今天把這部分稍作小結。

       使用spring定時任務的前提:項目中已經搭建好了spring環境(我用的是spring3.0)。

       一、基本使用:

        spring的定時任務使用起來十分方便,只需要兩步:1、寫好執行定時任務的類和方法;2、配置spring定時任務設定檔:

        1、寫好執行定時任務的類和方法:

package com.test;public class Test {public void test() {System.out.println("執行定時任務的方法。");}}

        2、配置spring的定時任務設定檔(可以建立一個xml檔案,也可以在已經有的xml檔案中配置)

設定檔內容:

<?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:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">   <bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">       <!--必須,QuartzScheduler 延時啟動,應用啟動後 QuartzScheduler 再啟動--><property name="startupDelay" value="60"/><!-- 普通觸發器 :觸發器列表--><property name="triggers"><list><ref local="testTrigger"/></list></property>    </bean><!-- 配置執行定時任務的類和方法 -->     <bean id="testDetail"          class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">          <property name="targetObject">              <bean class="com.test.Test"></bean>        </property>          <property name="targetMethod">              <value>test</value>          </property>      </bean>
<!-- 配置觸發器 -->       <bean id="testTrigger"          class="org.springframework.scheduling.quartz.CronTriggerBean">          <property name="jobDetail">              <ref bean="testDetail"/> <!-- 觸發器觸發的 執行定時任務的bean -->           </property>          <property name="cronExpression">              <!-- 每天23時   -->  <!-- 定時任務執行的間隔 -->             <value>0 0 23 * * ?</value>        </property>      </bean>  </beans>

       二、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.