spring schduled的简单使用

6 篇文章 0 订阅

1. 创建一个springboot项目

这是spring3.0以后的功能哈,如果要是用的话,需要使用spring3.0以上版本
怎么创建springboot项目这还用我告诉你的话…别往下看了 你不适合敲代码 = =|||

2. pom中导入坐标

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>

3. 创建ScheduleDemo类

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

/**
 * @Author: guohui.wang
 * @Date: 2020-11-0217:51
 * @desc: scheduled示例类,类名你爱叫啥叫啥,不强制
 */
@Component
public class ScheduleDemo {
    /**
     * @desc: 定时任务方法,方法名你爱叫啥叫啥,不强制
     *
     * <pre>
     *   @Scheduled: 标明这个方法是个定时任务方法
     *     cron:crom表达式
     * </pre>
     */
    @Scheduled(cron = "* * * * * ?")
    public void scheduleMethod() {
    	// 这里写你的逻辑,你要定时调度干啥东西写在这
        System.out.println("scheduleMethod running...");
    }
}

cron表达式参考:https://www.cnblogs.com/junrong624/p/4239517.html

4. 启动类加EnableScheduling注解

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

@SpringBootApplication
@EnableScheduling    //<------没错,加的就是它
public class ScheduledApplication {
    public static void main(String[] args) {
        SpringApplication.run(ScheduledApplication.class, args);
    }
}

5. 运行程序

程序运行截图

到此为止,我们的简单demo就已经编写好了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值