提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录- 学习目标:
- 前言
- 一、SpringBoot 整合定时任务task
- 二、使用步骤
- 1.引入库
- 2.读入数据
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
提示:以下是本篇文章正文内容,下面案例可供参考
一、SpringBoot 整合定时任务task使用注解@EnableScheduling开启定时任务,会自动扫描
定义@Component作为组件被容器扫描
代码如下(示例):
@SpringBootApplication
//开启定时任务
@EnableScheduling
//扫描mybatis mapper 包路径
@MapperScan(basePackages = “com.eci.mapper”)
//扫描所有需要的包,包含一些自用的工具类包所有路径
@ComponentScan(basePackages = {“com.eci.dom”})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2.读入数据代码如下(示例):
@Component
public class TesetTask {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat(“HH:mm:ss”);
//每三秒打印一次
//@Scheduled(fixedDelay = 3000)
//使用表达式
@Scheduled(cron = “4-40 * * * * ?”)
public void reportCurrentTime() {
System.out.println(“现在时间:” + dateFormat.format(new Date()));
}
}



