springBoot 入门(六)—— 整合Spring框架开启自带的任务调度器执行任务(注解方式)

时间:2022-07-25
本文章向大家介绍springBoot 入门(六)—— 整合Spring框架开启自带的任务调度器执行任务(注解方式),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

###1.首先需要在配置中开启spring框架自带的任务调度器,开启代码如下

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration
@EnableScheduling
public class SpringScheduleConfig {

}

###2.在需要使用的地方使用注解@Scheduled来配置任务的具体调度时间等

package schedule;

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

@Component
public class TestTask {
    @Scheduled(cron = "0 * 14 * * ? ") // 14点每分执行的任务
    public void minuteJob() {
        System.out.println("do somthing....");
    }
}