本例场景:
springboot集成task
本例demo源码: https://github.com/zhangbeizhen/springboot-task
1.在SpringbootTaskApplication使用@EnableScheduling注解启动定时功能
@EnableScheduling /**开启基于注解的定时任务*/
@SpringBootApplication
public class SpringbootTaskApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTaskApplication.class, args);
}
}
2.在ScheduledService使用@Scheduled注定配置定时任务
@Service
public class ScheduledService {
private static final Logger logger = LoggerFactory.getLogger(ScheduledService.class);
@Scheduled(cron = "0/5 * * * * ?") //每5秒执行一次
public void doWork(){
logger.info("测试开始......");
logger.info("正在执行......");
logger.info("测试结束......");
}
}
3.测试截图
以上,TKS.