@Scheduled 语法学习记录
- [@Scheduled(cron = "1 0 0 * * ?")](#@Scheduled(cron = "1 0 0 * * ?"))
@Scheduled(cron = "1 0 0 * * ?")
1 .@Scheduled 方法的使用 说明
javascript
//0 0 * * * ? 每小时执行一次
//0 0 */1 * * ? 每小时执行一次
//* * 0/1 * * ? 每小时执行一次
//0 0/2 * * * ? 每两分钟
//0/1 * * * * ? 每一秒执行一次
//0 0 0/1 * * ? 每小时执行一次
//0/20 * * * * ? 每20秒执行一次
// @Scheduled(cron = "0 0 0 * * ?") // 每天零点执行一次
@Scheduled(cron = "1 0 0 * * ?") // 每天零点零一分执行一次
public void performTask() {
System.out.println("Task executed at 00:01");
// 你的业务逻辑代码写在这里
}
Cron表达式解释:
0 秒
1 分(即第1分钟)
0 小时(即零点)
* 日(每一天)
* 月(每一个月)
? 星期中的天(不指定,因为我们已经指定了日)
- 启用定时任务
在你的主类或者配置类上添加@EnableScheduling注解来启用定时任务支持:
javascript
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
-
添加依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.x.x.RELEASE</version> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
// 参考 百度Ai https://www.baidu.com