入口类添加 @EnableScheduling
// 允许定时器
@EnableScheduling
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }
}
定时器:
注意不要忘了加@Component
package com.example.demo.util;
import com.example.demo.service.ItemService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduTask {
private ItemService itemService = new ItemService();
@Scheduled(cron = "0/5 * * * * *")
public void testSca(){
Thread thread = Thread.currentThread();
String name = thread.getName();
//getCompareTimestamp
itemService.getCompareTimestamp();
System.out.println("测试 name" + name);
}
}
cron语法参考:
https://blog.csdn.net/weixin_40085570/article/details/80581815