> 文档中心 > springBoot后端代码中使用定时器@Scheduled注解

springBoot后端代码中使用定时器@Scheduled注解

第一步,在启动类中添加@EnableScheduling(必须)

/ * 启动程序 *  */@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })@EnableSchedulingpublic class RuoYiApplication{    public static void main(String[] args)    {    }}

第二部,在要执行定时器的方法上添加@Scheduled(七子表达式)

    @Scheduled(cron="0/1 * * * * ? ")    @GetMapping("/list")    public void test01() throws Exception{// batchPaymentService.t();// batchPaymentService.doTaskOne();// batchPaymentService.doTaskTwo();// batchPaymentService.doTaskThree(); System.out.println("执行完了");    }

//执行结果,每一秒执行一次