天天看點

Spring定時任務( Spring-Task)用法

第一種:配置檔案方式
第一步:編寫作業類
即普通的pojo,如下:
import org.springframework.stereotype.Service;  
@Service  
public class TaskJob {  

    public void job1() {  
        System.out.println(“任務進行中。。。”);  
    }  
}  
 第二步:在spring配置檔案頭中添加命名空間及描述
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:task="http://www.springframework.org/schema/task"   
    。。。。。。  
    xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">  
 第三步:spring配置檔案中設定具體的任務
 <task:scheduled-tasks>   
        <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>   
</task:scheduled-tasks>  
  <context:component-scan base-package=" com.gy.mytask " />  
說明:ref參數指定的即任務類,method指定的即需要運作的方法,cron及cronExpression表達式,具體寫法這裡不介紹了,詳情見上篇文章附錄。
<context:component-scan base-package="com.gy.mytask" />這個配置不消多說了,spring掃描注解用的。