前言
-
传统定时器是硬编码。但是有的时候业务上需要不断地调整
问题描述
-
我们开发了一个定闹钟的功能。这个功能肯定是定时器开发。但是这就存在一个问题这个定时是动态的。那么我们如何实现呢?请接着看
简介
-
定时器在开发中真的算是一种福利了。通过定时器我们省去了很多人力。我们通过定时器将一些繁琐定期的事情通过代码去完成。在Java开发中我们通过Timer类可以简单实现定时器功能。既然是springboot课程今天我们就来看看srpingboot整合定时器的事情
传统定时器
-
这里使用的是之前课程一的配置。springboot打算是系列讲解。所以配置都是承前启后的。建议大家按顺序观看。
@Componentpublic class SimpleSchedule { @Autowired TestMapper testMapper; @Scheduled(cron = "*/6 * * * * ?") private void process() { List<test> tests = testMapper.getTests(); System.out.println(tests); }}
-
定时器的编写也很简单,只需要在类或者方法上加上@Scheduled注解。然后配置cron表达式就可以了。这里得注意一下需要在spirngboot启动类上加上开发定时器的注解。
@SpringBootApplicationpublic class CrontabApplication { public static void main(String[] args) { SpringApplication.run(CrontabApplication.class,