23.8 ApplicationRunner 또는 CommandLineRunner 사용

만약 SpringApplication이 시작할 때 특정 코드를 한번 실행 시킬 필요가 있으면 ApplicationRunnerCommandLineRunner 인터페이스를 implements 할 수 있다. 두 인터페이스는 같은 방식으로 작동하며 SpringApplication.run(...) 메소드 실행이 완료되기 직전에 호출 되는 run 메소드를 하나 제공 한다. CommandLineRunner 인테페이스는 어플리케이션 인수를 한개의 스트링 배열로 접근해 사용 하는 방법을 제공 하지만 ApplicationRunner는 이전에 우리가 봤던 ApplicationArguments인터페이스를 사용한다.

import org.springframework.boot.*
import org.springframework.stereotype.* 
@Component 
public class MyBean implements CommandLineRunner { 
   	public void run(String... args) { 		
       // Do something... 	
   }
}

CommandLineRunnerApplicationRunner빈들을 여러개 정의 하고 그들의 실행 순서를 정의 해야 하는 경우 추가적으로 org.springframework.core.Ordered 인터페이스를 구현하거나 org.springframework.core.annotation.Order 어노테이션을 사용할 수 있다.

Last updated