1. 创建一个springboot项目
这是spring3.0以后的功能哈,如果要是用的话,需要使用spring3.0以上版本
怎么创建springboot项目这还用我告诉你的话…别往下看了 你不适合敲代码 = =|||
2. pom中导入坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
3. 创建ScheduleDemo类
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @Author: guohui.wang
* @Date: 2020-11-0217:51
* @desc: scheduled示例类,类名你爱叫啥叫啥,不强制
*/
@Component
public class ScheduleDemo {
/**
* @desc: 定时任务方法,方法名你爱叫啥叫啥,不强制
*
* <pre>
* @Scheduled: 标明这个方法是个定时任务方法
* cron:crom表达式
* </pre>
*/
@Scheduled(cron = "* * * * * ?")
public void scheduleMethod() {
// 这里写你的逻辑,你要定时调度干啥东西写在这
System.out.println("scheduleMethod running...");
}
}
cron表达式参考:https://www.cnblogs.com/junrong624/p/4239517.html
4. 启动类加EnableScheduling注解
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling //<------没错,加的就是它
public class ScheduledApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduledApplication.class, args);
}
}
5. 运行程序
到此为止,我们的简单demo就已经编写好了。