복붙노트

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

SPRING

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

다음 ApplicationListener 있습니다.

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 정의 :

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

나는 콩이 생성자로부터의 메시지가 인쇄 될 때 생성된다는 것을 알 수 있지만, 문맥 시작 이벤트는 결코 수신되지 않는다. 내가 뭘 놓치고 있니?

해결법

  1. ==============================

    1.컨텍스트에서 ConfigurableApplicationContext.start ()를 명시 적으로 호출하면 ContextStartedEvent가 게시됩니다. 컨텍스트가 초기화 될 때 게시되는 이벤트가 필요하면 ContextRefreshedEvent를 사용하십시오.

    컨텍스트에서 ConfigurableApplicationContext.start ()를 명시 적으로 호출하면 ContextStartedEvent가 게시됩니다. 컨텍스트가 초기화 될 때 게시되는 이벤트가 필요하면 ContextRefreshedEvent를 사용하십시오.

    참조 :

  2. ==============================

    2.게으른로드 된 bean이 없기 때문에 (아마도) 당신은 틀린 이유 때문에 이벤트를 사용할 가능성이 높으며 아마 InitializingBean 인터페이스와 같은 것을 사용해야 할 것입니다 :

    게으른로드 된 bean이 없기 때문에 (아마도) 당신은 틀린 이유 때문에 이벤트를 사용할 가능성이 높으며 아마 InitializingBean 인터페이스와 같은 것을 사용해야 할 것입니다 :

    public class MyBean implements InitializingBean {
    
        @Override
        public void afterPropertiesSet() throws Exception {
            // ...
        }
    
    }
    

    스프링 매뉴얼에서 :

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

  3. ==============================

    3.이것이 도움이되는지 확실하지 않지만 비슷한 문제가 있음을 막연하게 기억합니다.이 문제는 지연로드가 아니라 미리로드로 해결되었습니다. 두 가지에 대한 간략한 개요는 다음과 같습니다.

    이것이 도움이되는지 확실하지 않지만 비슷한 문제가 있음을 막연하게 기억합니다.이 문제는 지연로드가 아니라 미리로드로 해결되었습니다. 두 가지에 대한 간략한 개요는 다음과 같습니다.

  4. from https://stackoverflow.com/questions/5728376/spring-applicationlistener-is-not-receiving-events by cc-by-sa and MIT license