【springboot】整合task定时任务

本文介绍了一个使用Spring Boot实现的简单定时任务应用示例。该示例通过@EnableScheduling启用定时任务支持,并利用@Scheduled注解定义了一个每秒执行的任务。此任务在控制台输出一条消息,展示了如何在Spring Boot应用中集成定时任务。
效果图

在这里插入图片描述

代码示例
package com.learning;

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

/**
 * @Author wangyouhui
 * @Date 2022/12/17 20:29
 * @Description TODO
 **/
@SpringBootApplication
// 加上EnableScheduling注解
@EnableScheduling
public class SpringBootLearningApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootLearningApplication.class, args);
    }
}

package com.learning.task;

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

/**
 * @Author wangyouhui
 * @Description 定时任务
 **/
@Component
public class MyTask {
    //秒 分 时 日期 月份 星期  年(非必须)
    @Scheduled(cron = "0/1 * * * * ?")
    public void print(){
        System.out.println("task run...");
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佑辉

老板,赏点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值