💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
使用定时任务的步骤如下: **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 ... "); } } ```