programing

Spring ApplicationListener가 이벤트를 수신하지 않습니다.

css3 2023. 10. 27. 22:05

Spring ApplicationListener가 이벤트를 수신하지 않습니다.

다음과 같은 Application Listener가 있습니다.

package org.mycompany.listeners;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;

public class MyApplicationListener implements ApplicationListener<ContextStartedEvent> {

  public MyApplicationListener() {
    super();
    System.out.println("Application context listener is created!");
  }

  /**
   * {@inheritDoc}
   */
  public void onApplicationEvent(final ContextStartedEvent event) {
    System.out.println("Context '" + event.getApplicationContext().getDisplayName() + "' is started!");
  }

}

그리고 다음과 같은 콩의 정의:

<bean name="myApplicationListener" class="org.mycompany.listeners.MyApplicationListener" />

컨스트럭터의 메시지가 출력되면 빈이 생성되지만 컨텍스트 시작 이벤트는 수신되지 않는 것을 알 수 있습니다.제가 무엇을 빠뜨리고 있나요?

ContextStartedEvent를 명시적으로 호출할 때 게시됩니다.ConfigurableApplicationContext.start()상황에 따라컨텍스트가 초기화될 때 게시되는 이벤트가 필요한 경우 다음을 사용합니다.ContextRefreshedEvent.

참고 항목:

게으르게 로드된 콩이 없기 때문에 잘못된 이유로 이벤트를 사용할 가능성이 크며 대신 빈 인터페이스 초기화와 같은 것을 사용해야 할 가능성이 높습니다.

public class MyBean implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        // ...
    }

}

스프링 설명서에서:

컨테이너의 빈 라이프사이클 관리와 상호 작용하기 위해 Spring Initializing Bean 및 Disposable Bean 인터페이스를 구현할 수 있습니다.이 컨테이너는 전자의 PropertiesSet()을 호출하고 후자의 경우 파괴()를 호출하여 초기화 및 원두 파괴 시 원두가 특정 동작을 수행할 수 있도록 합니다.또한 init-method 및 destroy 메서드 개체 정의 메타데이터를 사용하여 클래스를 Spring 인터페이스에 연결하지 않고도 컨테이너와 동일한 통합을 달성할 수 있습니다.

출처: Spring Framework - 라이프사이클 콜백

이게 도움이 될지는 모르겠지만, 게으르지 않고 선로딩으로 해결된 비슷한 문제가 있었던 것을 어렴풋이 기억하고 있습니다.다음은 둘 다에 대한 간단한 개요입니다.

언급URL : https://stackoverflow.com/questions/5728376/spring-applicationlistener-is-not-receiving-events