@Scheduled(cron = “1 0 0 * * ?“用法介绍

@Scheduled 语法学习记录

  • [@Scheduled(cron = "1 0 0 * * ?")](#@Scheduled(cron = "1 0 0 * * ?"))

@Scheduled(cron = "1 0 0 * * ?")

1 .@Scheduled 方法的使用 说明

javascript 复制代码
    //0 0 * * * ?     每小时执行一次
    //0 0 */1 * * ?   每小时执行一次
    //* * 0/1 * * ?   每小时执行一次

    //0 0/2 * * * ?   每两分钟

    //0/1 * * * * ?  每一秒执行一次
    //0 0 0/1 * * ?   每小时执行一次
    //0/20 * * * * ?  每20秒执行一次


   // @Scheduled(cron = "0 0 0 * * ?") // 每天零点执行一次


    @Scheduled(cron = "1 0 0 * * ?") // 每天零点零一分执行一次
    public void performTask() {
        System.out.println("Task executed at 00:01");
        // 你的业务逻辑代码写在这里
    }
    
复制代码
Cron表达式解释:
0 秒
1 分(即第1分钟)
0 小时(即零点)
* 日(每一天)
* 月(每一个月)
? 星期中的天(不指定,因为我们已经指定了日)
  1. 启用定时任务
    在你的主类或者配置类上添加@EnableScheduling注解来启用定时任务支持:
javascript 复制代码
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableScheduling
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
  1. 添加依赖

    org.springframework.boot spring-boot-starter-parent 2.x.x.RELEASE org.springframework.boot spring-boot-starter

// 参考 百度Ai https://www.baidu.com

相关推荐
小羊没烦恼!21 小时前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
俊昭喜喜里1 天前
java中的继承和多态的区别
java
小羊没烦恼!1 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕1 天前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码1 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖1 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时1 天前
01-06-A-JVM排查实战详解
java·jvm
vipxieliang1 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot
ba_pi1 天前
mysql 查询所有表名并授权
java
ProcessOn官方账号1 天前
java函数式编程--入门基础
java·编程·函数式编程