调用service层的方法,设置定时执行。
- 定时任务类( @Scheduled )
@Component
@Log4j2
public class XXXScheduledClass {
@Autowired
private XXXService xxxService;
@Scheduled(cron = "0 0/1 * * * ? ")
//一分钟执行一次
public void xxxMethod(){
try {
log.info("start:{}", DateUtil.now());
xxxService.get();
}catch (Exception e){
e.printStackTrace();
log.info("end:{}", DateUtil.now());
}
}
}
- 启动类( @EnableScheduling )
@SpringBootApplication
@EnableScheduling
public class StartApplication {
public static void main(String[] args) {
SpringApplication.run(StartApplication .class, args);
}
}