SpringBoot整合task实现定时任务、@EnableScheduling、@Scheduled

目录

一 步骤

1 引导类

2 bean

(3 task的配置)


定时任务应用场景:年度报表、秒杀商品上架、缓存统计报告

task就是spring的

一 步骤


1 引导类

加注解@EnableScheduling:开启定时任务功能
    


2 bean


    把当前bean注入spring容器
    任务上面加@Scheduled注解

package com.qing.bean;

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

@Component
public class MyBean {

    //cron = "秒 分 时 日 月 星期"
    //cron = "0/1 * * * * ?"   :从0秒开始每1s执行一次
    @Scheduled(cron = "0/1 * * * * ?")
    public void print(){
        System.out.println("spring task run ...");
    }

}

完事了

(3 task的配置)

 试一下线程前缀名

 

 

 

猜你喜欢

转载自blog.csdn.net/m0_45877477/article/details/125565503