ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring Boot의 주요 이벤트 정리
    Language/Java 2024. 12. 27. 17:41

    aws 서버에 소스를 배포하기 전후 처리를 appspec.yml에서 ApplicationStart 및 ValidateService hooks을 사용해서 처리하려다가 스프링에서 처리하는게 안정적이고 효과적일듯 하여 정리해 둔다.

     

    Spring Boot의 주요 이벤트

    이벤트 실행 순서대로 정리해 보았다.

    1. ApplicationStartingEvent

    • 애플리케이션이 가장 처음 시작될 때 발생
    • 환경 변수나 다른 설정들이 로드되기 전에 발생
    • 로깅 초기화 외의 어떤 처리도 하기 전 상태

    2. ApplicationEnvironmentPreparedEvent

    • 환경 변수들이 준비된 후 발생
    • application.properties/yml 파일이 로드된 시점
    • 아직 ApplicationContext는 생성되기 전 상태

    3. ApplicationContextInitializedEvent

    • ApplicationContext가 생성되고 초기화된 후 발생
    • 하지만 아직 빈 정의들이 로드되기 전 상태

    4. ApplicationPreparedEvent

    • ApplicationContext가 준비된 후 발생
    • 빈 정의들이 로드되었지만, 아직 빈 인스턴스는 생성되기 전 상태

    5. ContextRefreshedEvent

    • ApplicationContext가 리프레시될 때 발생
    • 모든 빈들이 초기화된 후 발생

    6. ApplicationStartedEvent

    • ApplicationContext가 리프레시된 후 발생
    • 애플리케이션이 요청을 처리할 준비가 되었지만
    • ApplicationRunner와 CommandLineRunner는 아직 실행되기 전

    7. AvailabilityChangeEvent

    • 애플리케이션의 준비 상태가 변경될 때 발생
    • LivenessState와 ReadinessState 상태 변화 알림

    8. ApplicationReadyEvent

    • 모든 초기화 작업이 완료된 후 발생
    • ApplicationRunner와 CommandLineRunner가 실행된 후
    • 애플리케이션이 완전히 실행 준비된 상태

    9. ContextStartedEvent

    • ApplicationContext가 start() 메소드로 시작될 때 발생
    • 주로 웹 애플리케이션에서 발생

    10. ContextStoppedEvent

    • ApplicationContext가 stop() 메소드로 중지될 때 발생
    • 애플리케이션이 아직 살아있지만 일시 중지된 상태

    11. ContextClosedEvent

    • ApplicationContext가 close() 메소드로 종료될 때 발생
    • 애플리케이션이 종료되기 직전

    12. ApplicationFailedEvent

    • 시작 과정 중에 예외가 발생했을 때 발생
    • 애플리케이션 시작 실패 시의 마지막 이벤트

     

    소스코드

    • ApplicationLifecycleManager
    package net.neoflat.common.core.config;
    
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import net.neoflat.common.service.TestService;
    import org.springframework.boot.availability.AvailabilityChangeEvent;
    import org.springframework.boot.context.event.ApplicationContextInitializedEvent;
    import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
    import org.springframework.boot.context.event.ApplicationFailedEvent;
    import org.springframework.boot.context.event.ApplicationPreparedEvent;
    import org.springframework.boot.context.event.ApplicationReadyEvent;
    import org.springframework.boot.context.event.ApplicationStartedEvent;
    import org.springframework.boot.context.event.ApplicationStartingEvent;
    import org.springframework.context.event.ContextClosedEvent;
    import org.springframework.context.event.ContextRefreshedEvent;
    import org.springframework.context.event.ContextStartedEvent;
    import org.springframework.context.event.ContextStoppedEvent;
    import org.springframework.context.event.EventListener;
    import org.springframework.stereotype.Component;
    
    @Slf4j
    @Component
    @RequiredArgsConstructor
    public class ApplicationLifecycleManager {
      private final TestService testService;
    
      @EventListener(ApplicationStartingEvent.class)
      public void onApplicationStartingEvent(ApplicationStartingEvent event) {
        log.info("@@@ 1. onApplicationStartingEvent !!!");
      }
    
      @EventListener(ApplicationEnvironmentPreparedEvent.class)
      public void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
        log.info("@@@ 2. onApplicationEnvironmentPreparedEvent !!!");
      }
    
      @EventListener(ApplicationContextInitializedEvent.class)
      public void onApplicationContextInitializedEvent(ApplicationContextInitializedEvent event) {
        log.info("@@@ 3. onApplicationContextInitializedEvent !!!");
      }
    
      @EventListener(ApplicationPreparedEvent.class)
      public void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
        log.info("@@@ 4. onApplicationPreparedEvent !!!");
      }
    
      @EventListener(ContextRefreshedEvent.class)
      public void onContextRefreshedEvent(ContextRefreshedEvent event) {
        log.info("@@@ 5. onContextRefreshedEvent !!!");
      }
    
      @EventListener(ApplicationStartedEvent.class)
      public void onApplicationStartedEvent(ApplicationStartedEvent event) {
        log.info("@@@ 6. onApplicationStartedEvent !!!");
      }
    
      @EventListener(AvailabilityChangeEvent.class)
      public void onAvailabilityChangeEvent(AvailabilityChangeEvent event) {
        log.info("@@@ 7. onAvailabilityChangeEvent !!!");
      }
    
      @EventListener(ApplicationReadyEvent.class)
      public void onApplicationReadyEvent(ApplicationReadyEvent event) {
        // TODO. work
        log.info("@@@ 8. onApplicationReadyEvent !!!");
        testService.test();
      }
    
      @EventListener(ContextStartedEvent.class)
      public void onContextStartedEvent(ContextStartedEvent event) {
        log.info("@@@ 9. onContextStartedEvent !!!");
      }
    
      @EventListener(ContextStoppedEvent.class)
      public void onContextStoppedEvent(ContextStoppedEvent event) {
        log.info("@@@ 10. onContextStoppedEvent !!!");
      }
    
      @EventListener(ContextClosedEvent.class)
      public void onContextClosedEvent(ContextClosedEvent event) {
        // TODO. work
        log.info("@@@ 11. onContextClosedEvent !!!");
        testService.test();
      }
    
      @EventListener(ApplicationFailedEvent.class)
      public void onApplicationFailedEvent(ApplicationFailedEvent event) {
        log.info("@@@ 12. onApplicationFailedEvent !!!");
      }
    }

     

    • TestService
    package net.neoflat.common.service;
    
    import java.util.Optional;
    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import net.neoflat.common.entity.AttacheFile;
    import net.neoflat.common.repository.AttacheFileRepository;
    import org.springframework.stereotype.Service;
    
    @Slf4j
    @Service
    @RequiredArgsConstructor
    public class TestService {
    
      private final AttacheFileRepository attacheFileRepository;
    
      public void test() {
        Optional<AttacheFile> optionalAttacheFileEntity = attacheFileRepository.findById(227L);
        optionalAttacheFileEntity.ifPresent(attacheFile -> log.info(attacheFile.getFileName()));
      }
    }

     

     

    필요한 이벤트 영역에 코드를 추가하면 되겠다.

     

     

     

    댓글

Designed by Tistory.