例如,每天都会运行此函数。如果我能传递参数就好了。
最简单的方法是使用
@Scheduled
。
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.Instant;
@Component
@EnableScheduling
public class Scheduler {
@Scheduled(cron = "0 30 8 * * ?", zone = "UTC")
public void eightAMRun() {
System.out.println("Breakfast time - " + Instant.now());
}
}
"0 30 8 * * ?"
是一个 cron 表达式,表示“每天上午 08:30:00”