基于springboot的后台定时任务创建步骤
- 在启动类标注注解@EnableScheduling,表明此类 存在定时任务
@EnableScheduling
@SpringBootApplication
@MapperScan("com.example.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 新建类,记得类要添加@Component注解,对定时执行方法添加@Scheduled(cron =cron定时表达式)
package com.example.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class SchedulTask {
@Scheduled(cron ="*/1 * * * * ?")
public void test() {
System.out.println("hello");
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a005d6719a49106ce1693a019458304e.jpeg)