一:在程序入口类中添加注解@EnableScheduling
@SpringBootApplication
@EnableScheduling
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
二:在一个没有带参数的方法上使用注解Scheduled
@Service
public class ScheduledTest {
@Scheduled(cron = "*/3 * * * * ?")
public void demoSchedule() {
System.out.println("执行");
}
}