SpringApplication(2)

오진현·2023년 3월 18일
0

Spring

목록 보기
3/7

Application Event

eventListener

SampleListern.class

@Component
public class SampleListener implements ApplicationListener<ApplicationStartingEvent> {
//ApplicationListener<이벤트 종류 타입 설정!!>

    @Override
    public void onApplicationEvent(ApplicationStartingEvent event) {
                
        System.out.println("======================");        
        System.out.println("Application is Starting");        
        System.out.println("======================");   
    }
}

중요한점!

ApplicationContext 가 생성 이전에 발생한 이벤트는 빈 등록시 없는 이벤트이므로..
리스너가 동작을 안함

이 경우는 등록을 해준다.

//Listner의 @Component 제거 후 
public static void main(String[] args) {
		SpringApplication app = new SpringApplication(DeanohApplication.class);
		app.run(args);

		app.addListeners(new SampleListener());  // <--
}        

setWepApplication

WepApplication 의 타입을 결정

MVC -> Servlet로 동작
Webflux -> Reactive로 동작
NONE -> MVC, Webflux 둘다 있을 경우 Servlet으로 동작
이경우, Webflux 사용 원한다면 Reactive 로 설정하면 됨.

app.setWebApplicationType(WebApplicationType.SERVLET);

Application argument 사용하기

DE환경


jvm option : -Dfoo
program arguments: --bar

// 빈에 생성자 한개이고, 그 생성자에 파라미터가 빈일 경우 그 빈은 스프링이 주입해준다.
@Component
public class argComponent {

    public argComponent(ApplicationArguments arguments){
        System.out.println("foo : "+ arguments.containsOption("foo"));
        System.out.println("foo : "+ arguments.containsOption("bar"));

    }
}

결과 :

program arguments 만 들어간다.

2.콘솔

명령어
java -jar target/deanoh-0.0.1-SNAPSHOT.jar -Dfoo --bar
결과:

--bar만 arguments 로 들어간다.

애플리케이션 실행 후

ApplicationRunner

CommandLineRunner

@Order(숫자)

ApplicationRunner 가 여러개일 경우 순서를 정할 수 있다.

profile
s나야

0개의 댓글