@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

相关推荐
ZhengEnCi4 小时前
J7A-高级Java工程师面试三道灵魂拷问-深度广度与工程素养的终极检验
java·后端
狼爷1 天前
吃透 Java Function 接口,搞定 99% 的 Stream 场景
java·函数式编程
祎雪双十Gy1 天前
从 DataX 的配置加载说起:我用 FastJson2 做了一个轻量级动态配置管理库
java·后端
小锋java12341 天前
分享一套锋哥原创的SpringBoot4+Vue3宠物领养网站系统
java
考虑考虑1 天前
Java实现hmacsha1加密算法
java·后端·java ee
掉鱼的猫1 天前
Spring Boot → Solon 注解迁移实战指南:一张对照表说清楚
java·spring boot
plainGeekDev1 天前
广播接收器 → Flow + Lifecycle
android·java·kotlin
plainGeekDev1 天前
EventBus → SharedFlow
android·java·kotlin
带刺的坐椅1 天前
Spring Boot → Solon 注解迁移实战指南:一张对照表说清楚
java·springboot·web·solon
用户3721574261351 天前
Java 将一个 PPT 文档拆分为多个文件
java