Spring Boot 计划任务(定时任务)

时间:2021-01-18 07:52:34

Spring Boot使用标签来实现计划任务(定时任务)

  • @EnableScheduling
  • @Component
  • @Qualifier
  • @Primary
  • @Scheduled

标签具体含义自行百度


FashionGoApplication类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class FashionGoApplication {

public static void main(String[] args) {
SpringApplication.run(FashionGoApplication.class, args);
}
}

任务类

@Component
@Qualifier
@Primary
public class Run {
@Scheduled(fixedRate = 3600000)
public void judgeItemOrder() {
System.out.println("每隔1小时执行一次,3600000 = 60 * 60 * 1000");
}
}