要执行的任务放入ioc容器 可支持多任务, 执行顺序是单线程
package com.peng.job; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDateTime; /** * @Author: wsp * @Data: 2022-12-19 17:29 */ @Component public class FileJob2 { @Scheduled(cron = "0/2 * * * * ?") public void FileJob() { System.out.println(LocalDateTime.now()+" ///"); } @Scheduled(cron = "0/4 * * * * ?") public void FileJob2() { System.out.println(LocalDateTime.now()+" #################"); } }
启动类添加注解 @EnableScheduling 扫描任务类
@EnableScheduling @SpringBootApplication public class WspApplicationApp { public static void main(String[] args) { SpringApplication.run(WspApplicationApp.class,args); } }