多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
使用定时任务的步骤如下: **1. 创建一个SpringBoot项目** ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` **2. 在启动类上标注注解`@EnableScheduling`开启定时任务支持** ```java @EnableScheduling @SpringBootApplication public class Springboot04TaskApplication { public static void main(String[] args) { SpringApplication.run(Springboot04TaskApplication.class, args); } } ``` **3. 在需要定时的类或方法上标注注解`@Scheduled`说明当前的类或方法是定时任务** ```java @Service public class ScheduledService { /** * @Scheduled 注解标记的方法不能有参数 */ @Scheduled(cron = "0/4 * * * * MON-SAT") //每4秒执行一次 public void hello() { System.out.println("hello ... "); } } ```