springboot cron定时任务配置

前一篇文章记录springboot配置mybatis。在基础上增加cron定时任务配置。

  • 开启定时任务
  • 任务代码实现
开启定时任务

开启定时任务比较简单,在入口出打开 @EnableScheduling

@SpringBootApplication
@MapperScan("com.hiro.demo.db")
@EnableScheduling
public class DemoApplication {
    public static void main(String [] args) {
        SpringApplication.run(DemoApplication.class,args);
    }
}
任务代码实现
package com.hiro.demo.timer;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Project: springbootDemo
 *
 * @author : hirolin
 * @date : 2019/3/25 22:44
 */
@Component
public class MainTimer {

    @Scheduled(cron = "${demo.backend.timer}")
    public void backendTimer(){
        System.out.println("test");
    }
}

其中 cron="${demo.backend.timer}" 是配置在application.yml中:

#定时任务执行
demo:
  backend:
      timer: "*/1 * * * * *"

项目运行后可看到控制台每秒打印:
```shell

cron常用
* * * * * * *
1 秒(0~59)
2 分钟(0~59)
3 小时(0~23)
4 天(0~31)
5 月(0~11)
6 星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
7.年份(1970-2099)
例如每秒钟:
" */1 * * * * *"
每天中午12点:
“0 0 12 * * ? " 每天中午12点触发

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值