본문 바로가기
Spring Boot

[Spring] 매일 자정 특정 동작 수행하기 (schedule, cron)

by palbokdev 2021. 10. 12.
@SpringBootApplication
@EnableScheduling // 스케쥴러 동작 가능하도록
public class DemoApplication extends SpringBootServletInitializer {

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(DemoApplication.class);
  }

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}
@Component
public class DemoScheduler {
  
  @Scheduled(cron = "0 0 0 * * *") // 매일 자정
  public void test() {
    // 수행할 동작
  }
}
@Scheduled(cron = "0 * * * * *") // 1분마다
@Scheduled(cron = "*/1 * * * * *") // 매초마다