如何使用Spring Boot的@Scheduled注解实现定时任务解析?

2026-06-10 10:031阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计578个文字,预计阅读时间需要3分钟。

如何使用Spring Boot的@Scheduled注解实现定时任务解析?

这篇文章主要介绍了Spring Boot定时任务@Scheduled的实现解析。通过示例代码展示了如何配置和使用,内容较为简练,对于希望深入了解Spring Boot定时任务的大家,具有一定的参考价值。需要的伙伴可以参考下文:

javaimport org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;

@Componentpublic class ScheduledTask {

@Scheduled(fixedRate=5000) public void scheduleFixedRateTask() { System.out.println(每5秒执行一次); }

@Scheduled(cron=0 0/30 * * * ?) public void scheduleCronTask() { System.out.println(每30分钟执行一次); }}

这篇文章主要介绍了springboot 定时任务@Scheduled实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、pom.xml中导入必要的依赖:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <dependencies> <!-- SpringBoot 核心组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> </dependencies>

2、写一个springboot的启动类:

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; @ComponentScan(basePackages = { "com.xwj.tasks" }) @EnableScheduling // 开启定时任务 @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }

注意这里一定要加上@EnableScheduling注解,用于开启定时任务

3、开始写定时任务:

import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduleTask { @Scheduled(fixedRate = 1000) // @Scheduled(cron = "0 23-25 18 * * ?") public void testSchedule() { System.out.println("定时任务:" + System.currentTimeMillis()); } }

解释:

@Scheduled注解:

1、fixedRate 以固定速率执行。以上表示每隔1秒执行一次

2、fixedDelay 以上一个任务开始时间为基准,从上一任务开始执行后再次调用

3、cron表达式。可以实现定时调用。

在使用的过程中,楼主觉得,如果只有一个定时任务,fixedRate与fixedDelay的效果是一样一样的

如何使用Spring Boot的@Scheduled注解实现定时任务解析?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计578个文字,预计阅读时间需要3分钟。

如何使用Spring Boot的@Scheduled注解实现定时任务解析?

这篇文章主要介绍了Spring Boot定时任务@Scheduled的实现解析。通过示例代码展示了如何配置和使用,内容较为简练,对于希望深入了解Spring Boot定时任务的大家,具有一定的参考价值。需要的伙伴可以参考下文:

javaimport org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;

@Componentpublic class ScheduledTask {

@Scheduled(fixedRate=5000) public void scheduleFixedRateTask() { System.out.println(每5秒执行一次); }

@Scheduled(cron=0 0/30 * * * ?) public void scheduleCronTask() { System.out.println(每30分钟执行一次); }}

这篇文章主要介绍了springboot 定时任务@Scheduled实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、pom.xml中导入必要的依赖:

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <dependencies> <!-- SpringBoot 核心组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> </dependencies>

2、写一个springboot的启动类:

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; @ComponentScan(basePackages = { "com.xwj.tasks" }) @EnableScheduling // 开启定时任务 @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }

注意这里一定要加上@EnableScheduling注解,用于开启定时任务

3、开始写定时任务:

import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduleTask { @Scheduled(fixedRate = 1000) // @Scheduled(cron = "0 23-25 18 * * ?") public void testSchedule() { System.out.println("定时任务:" + System.currentTimeMillis()); } }

解释:

@Scheduled注解:

1、fixedRate 以固定速率执行。以上表示每隔1秒执行一次

2、fixedDelay 以上一个任务开始时间为基准,从上一任务开始执行后再次调用

3、cron表达式。可以实现定时调用。

在使用的过程中,楼主觉得,如果只有一个定时任务,fixedRate与fixedDelay的效果是一样一样的

如何使用Spring Boot的@Scheduled注解实现定时任务解析?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。