spring 定時任務

在Spring配置文件中進行配置基本信息

在xmlns中添加

xmlns:task="http://www.springframework.org/schema/task"

 

在xsi中添加

http://www.springframework.org/schema/task

http://www.springframework.org/schema/task/spring-task-3.2.xsd

然後是必須的一項,因爲這個是spring提供的定時任務,所以需要進行掃描類下的包才能執行相應的操作

 

<!-- 配置掃描的包 我把相應的測試類放到com.test包下,這個一定要寫-->

<context:component-scan base-package="com.test"/>

然後是設置定時任務

<!-- 設置定時任務 -->
<task:annotation-driven/>

開始在測試類中寫測試代碼 

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
/**
 * Created by leo on 2018/1/22.
 */
@Component
public class TimerTask {
   
    @Scheduled(cron = "* * * * * ?")//每分鐘都執行
    public void test(){
        System.out.println("執行");
    }
 
   
}

Cron配置

 

CRON表達式 含義 
“0 0 12 * * ?” 每天中午十二點觸發 
“0 15 10 ? * *” 每天早上10:15觸發 
“0 15 10 * * ?” 每天早上10:15觸發 
“0 15 10 * * ? *” 每天早上10:15觸發 
“0 15 10 * * ? 2005” 2005年的每天早上10:15觸發 
“0 * 14 * * ?” 每天從下午2點開始到2點59分每分鐘一次觸發 
“0 0/5 14 * * ?” 每天從下午2點開始到2:55分結束每5分鐘一次觸發 
“0 0/5 14,18 * * ?” 每天的下午2點至2:55和6點至6點55分兩個時間段內每5分鐘一次觸發 
“0 0-5 14 * * ?” 每天14:00至14:05每分鐘一次觸發 
“0 10,44 14 ? 3 WED” 三月的每週三的14:10和14:44觸發 
“0 15 10 ? * MON-FRI” 每個週一、週二、週三、週四、週五的10:15觸發

 

 

 

 

 

 

 

 

借鑑同學博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章