1. 在主启动类添加注解
package com.muluo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableScheduling
@SpringBootApplication
public class QuartzApplication {
public static void main(String[] args) {
SpringApplication.run(QuartzApplication.class, args);
}
}
2. 编写定时任务类
package com.muluo.quartz;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @author Muluo
* @create 2022-04-07 10:45
*/
@Component
public class QuartzTask {
private int i = 0;
@Scheduled(cron = "*/5 * * * * ?")
public void printInfo() {
System.out.println(i++);
}
}
3. 运行结果
4. corn表达式解释
版权声明:本文为qq_43518425原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。