首页 > 编程语言 > 详细

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

时间:2021-05-04 10:04:45      阅读:26      评论:0      收藏:0      [点我收藏+]

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、直接启动项目即可;

 

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

原文:https://www.cnblogs.com/xie-qi/p/14728501.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!