一、添加ScheduledTaskService服务类
package com.uos.schedule.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Date;
@Service
public class ScheduledTaskService {
private static final SimpleDateFormat simpleDataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private Integer count1 = 1;
private Integer count2 = 1;
private Integer count3 = 1;
@Scheduled(fixedRate = 60000)
public void scheduledTaskImmediately() {
System.out.println(String.format("fixedRate第%s次执行,当时时间为:%s", count1++, simpleDataFormat.format(new Date())));
}
@Scheduled(fixedRate = 60000)
public void scheduledTaskAfterSleep() {
System.out.println(String.format("fixedDelay第%s次执行,当时时间为:%s", count2++, simpleDataFormat.format(new Date())));
}
@Scheduled(cron = "0 * * * * *")
public void scheduledTaskCron() {
System.out.println(String.format("Cron第%s次执行,当时时间为:%s", count3++, simpleDataFormat.format(new Date())));
}
}
二、在主程序类中添加@EnableScheduling注解

三、测试结果
