springBoot项目中任务---定时任务

1、在启动类上加上定时功能的注解

@EnableScheduling;
@EnableScheduling //开启定时功能的注解
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

2、写对应的定时器的实现方法;

@Service
public class ScheduledService {

    //在一个特定的时间执行这个方法  Timer
    //cron表达式
    //秒 分 时 日 月 周几(一到七)
    /*
    * 30 15 10 * * ?   每天10点15分30 执行一次
    * 30 0/5 10,18 * * ?  每天10点和18点,每隔5分钟执行一次
    * 0 15 10 ? * 1-6  每个月的周一到周六的10.15 执行一次
    * */
    @Scheduled(cron = "0 8 22 * * ?")
    public void hello(){
        System.out.println("hello,你被执行了~"+new Date());
    }

}

3、直接启动项目即可;

 

posted @ 2021-05-03 23:16  等你的夏天  阅读(138)  评论(0编辑  收藏  举报