示例五:springboot笔记示例五:集成定时任务
- 1、基于注解(@Scheduled)
- 2、基于接口(SchedulingConfigurer) 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库中读取指定时间来动态执行定时任务,这时候基于接口的定时任务就派上用场了。
- 3、基于注解设定多线程定时任务
方式一:基于注解@EnableScheduling
@SpringBootApplication
@MapperScan("com.example.scengine.dao")
@EnableScheduling //在配置类上使用,开启计划任务的支持
public class YdApplication {
public static void main(String[] args) {
SpringApplication.run(YdApplication.class, args);
}
}
创建定时任务
package com.example.scengine.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat<