1. spring boot的入口类Application.java中加入@EnableScheduling
package com.yunchuang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Demo8Application {
public static void main(String[] args) {
SpringApplication.run(Demo8Application.class, args);
}
}
2. 定时任务类
package com.yunchuang.schedule;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 定时器任务
*
* @author 尹冬飞
* @create 2017-04-12 8:24
*/
@Component
public class Jobs {
@Scheduled(fixedDelay = 1000)
public void job1(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
System.out.println(sdf.format(new Date()));
}
@Scheduled(cron = "0 10 3 ? * 1#3")
public void job2(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
System.out.println(sdf.format(new Date()));
}
}