Spring Boot - CommandLineRunner

이정수·2025년 10월 18일

Spring Boot

목록 보기
8/25

CommandLineRunner :
@SpringBootApplication을 통한 Spring Application의 구동 직후 실행해야하는 Spring Bean을 설정하기위해 사용하는 Interface
▶ 주로 Application 실행 즉시 초기화 하는 코드의 실행용도로 사용

  • run()
    CommandLineRunner구현메소드run() Method를 제공
    ▶ 해당 메소드 내부에 Application 구동 시 실행해야하는 코드를 정의

    String type의 가변 매개변수를 받는다.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class CourseJdbcCommandRunner implements CommandLineRunner {
    @Autowired
    private CourseJdbcRepository cjcr;
    @Override
    public void run(String... args) throws Exception {
        cjcr.insert();
    }
}

Spirng Application의 구동 즉시 해당 CourseJdbcCommandRunnerSpring Bean으로 등록되면서 run()을 통해 cjcr.insert()가 수행

profile
공부기록 블로그

0개의 댓글