1.让SpringBoot启动时自动执行代码
@Order(value=1)
@Component
public class InsertPursuanceBytime implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
running();
}
Order注解表明注入IOC容器的优先级,SpringBoot在启动服务后会自动运行run方法
2.开启定时服务
加上注解@EnableScheduling开启定时服务
加上注解@Configuration
@Scheduled(cron="0-59 * * * * ? ")
public void running(){
}
使用@Scheduled注解来表明running()方法是定时执行的方法,其中corn属性表明了自动执行周期,"秒,分,时,日,月,周,年"
此时在SpringBoot服务开启时优先启动run方法,然后在run方法里调用自己编写的running方法,就可以由running方法开启定时任务了。
WARING:@Scheduled注解下的方法不能有参数