这个就是比较简单的功能了 我选择的是利用注解@Schedule
默认,springboot已经支持了定时任务Schedule模块,所以一般情况已经完全能够满足我们的实际需求,一般来说,没有必要在加入其他类似于:quartz
1.在启动类添加注解@EnableScheduling
@SpringBootApplication @EnableScheduling public class HtmlsApplication { public static void main(String[] args) { SpringApplication.run(HtmlsApplication.class, args); } }
2.在controller层添加注解@Component
@Component @Controller @CrossOrigin @RequestMapping("htmls") public class XXXController { }
3.在需要定时的方法上添加@Scheduled
//@Scheduled(initialDelay = 1000, fixedRate = 6000) //第一次延迟1秒后执行,之后按fixedrate的规则每6秒执行一次 @Scheduled(cron ="0 0 10,14,16 * * ?") //每天上午10点,下午2点,4点
@RequestMapping(value = "ss",method = RequestMethod.GET) public CodeObjectResult selectHtmlsBySql(){ return htmlsService.selectHtmlOne(); }
cron表达式,有专门的语法,而且感觉有点绕人,不过简单来说,大家记住一些常用的用法即可,特殊的语法可以单独去查。
具体配置可以查看链接:https://www.jianshu.com/p/ef18af5a9c1d