添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 或者使用webflux -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
@EnableAsync
@EnableScheduling
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Slf4j
@Component
public class RunTask {
//cron表达式,每天0点执行
@Scheduled(cron = "0 0 0 * * ?", zone = "GMT+8")
public void run() {
try {
LocalDateTime dateTime = LocalDateTime.now();
log.info("》》》》》开始执行任务{} 》》》》》》");
log.info(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(dateTime));
} catch (Exception e) {
}
}
}