目录
一、springboot使用方法:
较为简单,直接上代码。
1.首先在springboot启动类上加上@EnableScheduling注解
示例:
@SpringBootApplication
@EnableScheduling
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
2.新建定时任务类,看代码:
@Component
public class ITask {
//每5秒执行一次
@Scheduled(cron = "0/5 * * * * ?")
public void printSay() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
String time = sdf.format(date);
System.out.println(time+"执行了一次");