Spring Boot 定时任务——@Scheduled

本文介绍了Spring Boot中的@Scheduled定时任务器,详细解析了如何配置和使用@Scheduled,以及Cron表达式的详细语法,包括星号(*)、问号(?)、减号(-)、逗号(,)、斜杠(/)、L、W、LW、井号(#)和C等特殊符号的含义和用法。" 132946365,20037466,R语言营养数据可视化实战,"['r语言', '信息可视化', '数据科学']
摘要由CSDN通过智能技术生成

Scheduled定时任务器:是Spring 3.0以后自带的一个定时任务器

目录

 

Scheduled的使用

Cron表达式


Scheduled的使用

  • 使用Scheduled我们首先添加其坐标
     <!-- 添加 Scheduled 坐标 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
  • 编写一个定时任务类
package com.zxw.scheduled;

import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class Scheduled {

    /*** 定时任务方法 * @Scheduled:设置定时任务
     * cron 属性:cron 表达式。定时任务触发是时间的一个字符串表达形式
     * 这里是每2秒执行一次
     */
    @org.springframework.scheduling.annotation.Scheduled(cron = "0/2 * * * * ?")
    public void test(){
        System.out.println("Scheduled:"+new Date());
    }
}
  • 在启动器中开启定时任务的使用
package com.zxw;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 启动类   定时任务
 */
@SpringBootApplication
@EnableScheduling //开启定时任务
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

至此,Scheduled就能正常使用了

Cron表达式

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值