天天看點

Springboot項目整合@Scheduled定時任務Springboot項目整合@Scheduled定時任務總結

Springboot項目整合@Scheduled定時任務

啟動類新增@EnableScheduling注解:

Springboot項目整合@Scheduled定時任務Springboot項目整合@Scheduled定時任務總結

建立定時任務類(Task.java):

package com.kd.opt.controller;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class TaskController {

    // 每5秒執行一次(秒、分、時、日、月、星期)
    @Scheduled(cron = "0/5 * *  * * ? ")
    public void method() {
        System.out.println("定時任務開始了:" + new Date().toGMTString());
    }
}
           

開始測試:

Springboot項目整合@Scheduled定時任務Springboot項目整合@Scheduled定時任務總結

總結

每天一個提升小技巧!!!

繼續閱讀