1.在启动类贴上@EnableScheduling
@SpringBootApplication
@PropertySource({"classpath:application-mgr.properties","classpath:redis.properties"})
@Import(ApplicationCoreConfig.class)
@EnableScheduling //定时标签
public class MgrsiteApplication {
public static void main(String[] args) {
SpringApplication.run(MgrsiteApplication.class, args);
}
2.在需要定时的方法上贴上@Scheduled(cron = “0 0 2 * * ?”)
/**
* 每天凌晨两点执行
*/
@Scheduled(cron = "0 0 2 * * ?")
public void DemoTask(){
System.out.println("执行定时任务");
}
3.完事
至此定时任务完成 ,就两个标签的事情;
@Scheduled(cron = “0 0 2 * * ?”) 所用到的 Cron表达式
再分享个 自动生成 Cron表达式的网站
http://cron.qqe2.com/