- 在线文档
- 项目结构
1.源码克隆:git clone https://github.com/spring-guides/gs-scheduling-tasks.git 2.包含两个项目initial和complete,initial可以根据文档练习完善,complete是完整项目 3.功能描述:构建应用程序,使用 Spring 的注释 @Scheduled 实现每五秒打印一次当前时间
-
源码分析
1.POM依赖 <dependencies> <!--https://blog.csdn.net/tongxin_tongmeng/article/details/128413623--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--https://blog.csdn.net/tongxin_tongmeng/article/details/128413783--> <dependency> <groupId>org.awaitility</groupId> <artifactId>awaitility</artifactId> <version>4.2.0</version> <scope>test</scope> </dependency> <!--https://blog.csdn.net/tongxin_tongmeng/article/details/128399703--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
2.入口类 // https://blog.csdn.net/tongxin_tongmeng/article/details/128401278 @SpringBootApplication // https://blog.csdn.net/tongxin_tongmeng/article/details/128414367 @EnableScheduling public class SchedulingTasksApplication { public static void main(String[] args) { SpringApplication.run(SchedulingTasksApplication.class); } }
3.任务类 @Component public class ScheduledTasks { private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class); private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @Scheduled(fixedRate = 5000) public void reportCurrentTime() { log.info("The time is now {}", dateFormat.format(new Date())); } }
- 项目演示
SpringBoot任务调度(官方案例)
于 2022-12-22 22:36:15 首次发布