springboot之定時任務

2022-06-08 09:09:08 字數 785 閱讀 8349

pom 包裡面只需要引入 spring boot starter 包即可

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

在啟動類上面加上@enablescheduling即可開啟定時

@enablescheduling

public

class

public

static

void

main(string args)

}定時任務1:

@component

public

class

schedulertask

}

定時任務2:

@component

public

class

scheduler2task

}

結果如下:

@scheduled引數可以接受兩種定時的設定,

一種是我們常用的cron="*/6 * * * * ?",

一種是fixedrate = 6000,兩種都表示每隔六秒列印一下內容。

fixedrate 說明

參考**

springboot任務之定時任務

1 service包下新建scheduleservice類 package com.example.springboottask.service import org.springframework.scheduling.annotation.scheduled import org.springf...

springboot任務之定時任務

在啟動入口上加上 enablescheduling 在需要定時的方法上加上 scheduled註解 比如 package com.gong.spingbootes.service import org.springframework.scheduling.annotation.scheduled i...

SpringBoot 定時任務

第一步 在啟動類中加入如下註解 enablescheduling public class public static void main string args 注意 enablescheduling必須加,否則無法開啟定時任務 第二步 建立定時任務 component public class ...