一:添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
二:在启动类上添加此注解@EnableScheduling
三:在测试定时累上加上@Component表示为交给spring容器管理
然后在要执行的方法上加@Scheduled(cron ="0/5 * * * * ?") 我这里用的是cron表达式 如果有要求凌晨00:00执行或者说每周执行一次每分钟执行一次不等的需求可以自行去在线生产cron表达式上自己找 我这里方便看测试效果是五秒钟执行一次
如果嫌麻烦可以贴@Scheduled(fixedRate = 5000)注解 这里是毫秒数或者@Scheduled(fixedDelay = 5000)两者意义我感觉相同
网友评论