Spring通過注解使用定時器

jopen 10年前發布 | 35K 次閱讀 Spring JEE框架

先需要在springMVC的配置文件中,配置定時器的掃描注解

<!-- 定時器開關 開始-->
 <task:annotation-driven />

定時器的實現代碼

package com.test.control.base;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class SpringTimerTest {

 /**
  * 啟動時執行一次,之后每隔3秒執行一次
  */
 @Scheduled(fixedRate = 1000 * 3)
 public void print() {
  System.out.println("timer running...");
 }
 
 /**
  * 定時啟動。每天凌晨 16:19 執行一次
  */
 @Scheduled(cron = "0 19 16 * * *")
 public void show() {
  System.out.println("定時器啟動...");
 }
}

最后寫個main方法測試下

public static void main(String[] args) {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("springmvcContext.xml");
 }
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!