如何在 Spring Boot 中启用定时任务

添加 @EnableScheduling 注解

可以添加在 Application 类上

java 复制代码
@SpringBootApplication
@EnableScheduling
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

定义定时任务类和方法

定时任务类要注册为Spring IoC 容器的 Bean,或者通过 @Bean 将某个方法的返回值(返回值为定时任务类对象)注册为 Spring IoC 的Bean。

java 复制代码
package com.cmcc.nlpt.sitemessage.task;

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

@Component
public class ScheduledTask {

    /**
     * 每日0点执行
     */
    @Scheduled (cron = "0 0 0 * * ?")
    public void doCronTask() {

    }

    /**
     * 本次任务结束到下次任务开始的时间间隔为5秒
     */
    @Scheduled (fixedDelay = 5000)
    public void doFixedDelayTask() {

    }

    /**
     * 两次任务开始时间间间隔为5秒
     */
    @Scheduled(fixedRate = 5000)
    public void doFixedRateTask() {

    }

    /**
     * 第一次任务启动延时1秒,后续任务结束到下次任务开始的时间间隔为5秒
     */
    @Scheduled(initialDelay = 1000, fixedDelay = 5000)
    public void doInitialDelayTask() {

    }
}

通过配置项控制定时任务是否开启

方法一 @ConditionalOnProperty

此方法是控制任务类是否注册为 Spring IoC 的 Bean,可以控制上述所有类型的定时任务是否注册。

yaml 复制代码
# enabled 配置为 false 时定时任务就不会开启
scheduled-task:
  enabled: true
java 复制代码
@Component
@ConditionalOnProperty(name = "scheduled-task.enabled", havingValue = "true")
public class ScheduledTask {
    /**
     * 每日0点执行
     */
    @Scheduled (cron = "0 0 0 * * ?")
    public void doCronTask() {

    }

    /**
     * 本次任务结束到下次任务开始的时间间隔为5秒
     */
    @Scheduled (fixedDelay = 5000)
    public void doFixedDelayTask() {

    }

    /**
     * 两次任务开始时间间间隔为5秒
     */
    @Scheduled(fixedRate = 5000)
    public void doFixedRateTask() {

    }

    /**
     * 第一次任务启动延时1秒,后续任务结束到下次任务开始的时间间隔为5秒
     */
    @Scheduled(initialDelay = 1000, fixedDelay = 5000)
    public void doInitialDelayTask() {

    }
}

方法二 Boolean 标志位

使用此方法定时任务还是会被调度执行,只是在任务执行时什么都不做,也可以控制上述所有类型的定时任务。

yaml 复制代码
scheduled-task:
  enabled: true
java 复制代码
@Component
@ConditionalOnProperty(name = "scheduled-task.enabled", havingValue = "true")
public class ScheduledTask {

    @Value("${scheduled-task.enabled}")
    private Boolean taskEnabled;

    @Scheduled (fixedDelay = 5000)
    public void doFixedDelayTask() {
        if (!taskEnabled) {
            return;
        }
        System.out.println("start to doFixedDelayTask");
    }
}

使用"-"使 cron 定时任务关闭

此方法只适用于 cron 类型的定时任务,不适用于其它类型的定时任务。

注意:yml 文件中配置 - 时要加上双引号或单引号,否则会报错。


关闭 cron 定时任务的配置:

yaml 复制代码
scheduled-task:
  cron: "-"

开始 cron 定时任务的配置,将 cron 表达式配置上即可:

yaml 复制代码
scheduled-task:
  cron: 0 0 0 * * ?
java 复制代码
@Component
public class ScheduledTask {
    /**
     * 每日0点执行
     */
    @Scheduled (cron = "${scheduled-task.cron}")
    public void doCronTask() {

    }
}

参考

相关推荐
全干engineer5 小时前
Spring Boot 实现主表+明细表 Excel 导出(EasyPOI 实战)
java·spring boot·后端·excel·easypoi·excel导出
a_Dragon15 小时前
Spring Boot多环境开发-Profiles
java·spring boot·后端·intellij-idea
ChinaRainbowSea6 小时前
补充:问题:CORS ,前后端访问跨域问题
java·spring boot·后端·spring
全栈凯哥9 小时前
02.SpringBoot常用Utils工具类详解
java·spring boot·后端
RainbowSea12 小时前
跨域问题(Allow CORS)解决(3 种方法)
java·spring boot·后端
RainbowSea12 小时前
问题 1:MyBatis-plus-3.5.9 的分页功能修复
java·spring boot·mybatis
sniper_fandc13 小时前
SpringBoot系列—入门
java·spring boot·后端
Albert Edison19 小时前
【最新版】IntelliJ IDEA 2025 创建 SpringBoot 项目
java·spring boot·intellij-idea
六毛的毛1 天前
Springboot开发常见注解一览
java·spring boot·后端
开开心心就好1 天前
免费PDF处理软件,支持多种操作
运维·服务器·前端·spring boot·智能手机·pdf·电脑