springboot开启定时任务

 

 

1.springboot程序入口添加注解

//主程序入口
@SpringBootApplication
@EnableScheduling //开启定时任务功能
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class,args);
    }
}

 

2.设置定时任务类

复制代码
@Component
public class MyTimer {

    @Scheduled(cron = "0/1 * * * * ?")
    public void print(){
        System.out.println("running....");
    }


}
复制代码

需要添加@Component让springboot接管

具体任务内容需要添加@Scheduled注解并配置cron任务时间

 

本文作者:Hello

本文链接:https://www.cnblogs.com/Hello233/p/15941563.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Hello霖  阅读(368)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起