SpringApplication :: Spring Boot
스프링 부트의 SpringApplication 이벤트 리스너는 애플리케이션의 시작과 종료 시점에서 발생하는 이벤트들을 처리할 수 있도록 지원하는 기능입니다. 애플리케이션의 특정 단계에서 이벤트 리스너를 사용하면, 환경 설정 로드, 애플리케이션 초기화, 에러 처리 등 다양한 작업을 수행할 수 있습니다.
스프링 부트 애플리케이션의 실행 과정에서 발생하는 주요 이벤트는 다음과 같습니다.
SpringApplication.run()이 호출된 직후에 발생하는 이벤트입니다.Environment)이 생성되고 구성된 후 발생하는 이벤트입니다.ApplicationContext가 초기화된 후 발생하는 이벤트입니다.ApplicationContext가 초기화된 후, 빈을 로드하기 직전에 발생하는 이벤트입니다.ApplicationContext가 초기화되고, 모든 빈이 생성된 후 발생하는 이벤트입니다.ApplicationListener 인터페이스를 구현하여 특정 이벤트를 처리하는 예제를 살펴보겠습니다. 예제에서는 애플리케이션 시작 후 ApplicationReadyEvent가 발생할 때 특정 작업을 수행하도록 설정합니다.
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationReadyListener implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
System.out.println("애플리케이션이 완전히 시작되었습니다. 초기화 작업을 수행합니다.");
// 초기화 작업 수행 예시
}
}
ApplicationReadyEvent를 수신하는 리스너로, 애플리케이션이 준비된 후 초기화 작업을 수행합니다.@Component로 등록했기 때문에 스프링 컨텍스트가 로드하면서 자동으로 리스너가 활성화됩니다.SpringApplication 객체를 생성하고 직접 이벤트 리스너를 등록하는 방법도 있습니다.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(DemoApplication.class);
// ApplicationStartingEvent 리스너 등록
app.addListeners(new ApplicationListener<ApplicationStartingEvent>() {
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
System.out.println("애플리케이션 시작 중...");
}
});
app.run(args);
}
}
ApplicationStartingEvent에 대해 리스너를 등록하여 애플리케이션이 시작될 때 메시지를 출력합니다.SpringApplication 객체의 addListeners() 메서드를 사용해 리스너를 등록합니다.애플리케이션의 다양한 이벤트 시점에서 작업을 수행하려면 여러 이벤트 리스너를 정의할 수 있습니다. 예를 들어, 애플리케이션 시작과 종료 시점을 구분하여 각각 리스너를 두면 다음과 같이 구성할 수 있습니다.
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class ApplicationStartedListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("애플리케이션이 시작되었습니다.");
}
}
@Component
public class ApplicationFailedListener implements ApplicationListener<ApplicationFailedEvent> {
@Override
public void onApplicationEvent(ApplicationFailedEvent event) {
System.out.println("애플리케이션 시작 중 오류가 발생했습니다: " + event.getException().getMessage());
}
}
스프링 부트의 SpringApplication 이벤트 리스너는 애플리케이션 실행 과정에서 발생하는 다양한 이벤트에 대해 특정 작업을 수행할 수 있게 도와줍니다. 주요 이벤트를 통해 애플리케이션의 시작과 종료, 예외 상황 등을 제어하고 원하는 작업을 손쉽게 추가할 수 있습니다.
package org.example.boot;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;
// ApplicationStartingEvent - 프로그램 시작되면
public class CustomListener1 implements ApplicationListener<ApplicationStartingEvent> {
@Override
public void onApplicationEvent(ApplicationStartingEvent event) {
System.out.println("Application starting");
}
}
//-
package org.example.boot;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
public class CustomListener2 implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("Application started");
}
}
//-
package org.example.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
@SpringBootApplication
public class Boot02Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Boot02Application.class);
app.addListeners(new CustomListener1(), new CustomListener2()); // 중간에 일어나는 이벤트 감시
app.run(args);
}
}