반응형
CommandLineRunner
- SpringBoot Application을 시작할 때 특정 코드를 실행하기 위한 인터페이스
- 이 인터페이스를 구현한 빈은, SpringBoot Application이 시작될때 실행된다.
예제
SayHello.java
package hello.hellospring;
import org.springframework.stereotype.Component;
@Component
public class SayHello {
public void say(String greeting) {
System.out.println("say hello : " + greeting);
}
}
- 간단한 인사말을 출력하는 클래스
HelloCommandLineRunnser.java
package hello.hellospring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class HelloCommandLineRunner implements CommandLineRunner {
@Autowired
private SayHello sayHello;
@Override
public void run(String... args) throws Exception {
sayHello.say("Hello!!!!");
}
}
- CommandLineRunner 인터페이스의 run() 메소드를 구현하고 있다.
- run() 메소드에서는 SayHello의 say() 메소드를 호출한다.
- 따라서, 이 코드는 SpringBoot 어플리케이션이 실행 시작 지점에,
SayHello의 say() 메소드를 호출한다.
결과
반응형