2016-07-07 1 views
2

스프링 ApplicationListener의 구현이 있습니다. 그것은 잘 작동하고 그것이 컨텍스트 XML 파일에서 콩으로 선언되거나 내가 @Component 주석을 사용하는 경우 이벤트를받습니다.코드를 통해 Spring ApplicationListener 구현을 등록하는 방법은 무엇입니까?

그러나 ConfigurableListableBeanFactoryregisterSingleton() 메서드를 사용하여 코드를 통해 수동으로 등록하면 이벤트가 수신되지 않습니다.

작동하는 경우와 작동하지 않는 경우를 설명하는 샘플 코드를 아래에 추가했습니다.

CustomEvent.java

package com.test.event; 

import org.springframework.context.ApplicationEvent; 

public class CustomEvent extends ApplicationEvent { 

    public CustomEvent(Object source) { 
     super(source); 
    } 

    public String toString() { 
     return "My Custom Event"; 
    } 
} 

CustomEventPublisher.java

package com.test.event; 

import org.springframework.context.ApplicationEventPublisher; 
import org.springframework.context.ApplicationEventPublisherAware; 

public class CustomEventPublisher implements ApplicationEventPublisherAware { 

    private ApplicationEventPublisher publisher; 

    public void setApplicationEventPublisher(ApplicationEventPublisher publisher) { 
     this.publisher = publisher; 
    } 

    public void publish() { 
     CustomEvent ce = new CustomEvent(this); 
     publisher.publishEvent(ce); 
    } 
} 

CustomEventHandler.java

package com.test.event; 

import org.springframework.context.ApplicationListener; 

public class CustomEventHandler 
    implements ApplicationListener<CustomEvent>{ 

    public void onApplicationEvent(CustomEvent event) { 
     System.out.println(event.toString()); 
    } 

} 

applicationContextWithListenerBean.xml

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

    <bean id="customEventHandler" 
     class="com.test.event.CustomEventHandler"/> 

    <bean id="customEventPublisher" 
     class="com.test.event.CustomEventPublisher"/> 

</beans> 

applicationContextWithoutListenerBean.xml

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

    <bean id="customEventPublisher" 
     class="com.test.event.CustomEventPublisher"/> 

</beans> 

MainApp.java

package com.test.event; 

import org.springframework.context.ConfigurableApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class MainApp { 
    public static void main(String[] args){ 

     /* The below code works fine when listener bean customEventHandler is defined in xml */ 

     ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
       "applicationContextWithListenerBean.xml"); 
     CustomEventPublisher cvp = (CustomEventPublisher) context 
       .getBean("customEventPublisher"); 
     cvp.publish(); 
     context.close(); 

     /* The below code doesn't work when listener bean is registered through code. Is it possible to make this work? */ 

     ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
       "applicationContextWithoutListenerBean.xml"); 
     context.getBeanFactory().registerSingleton("customEventHandler", 
       new CustomEventHandler()); 
     CustomEventPublisher cvp = (CustomEventPublisher) context 
       .getBean("customEventPublisher"); 

     cvp.publish(); 
     context.close(); 
    } 
} 

은 코드를 통해 ApplicationListener를 등록 할 수 없습니다?

답변

2

Bean을 싱글 톤으로 등록해도 ApplicationEvents에서 다시 호출 할 수 없습니다.

context.getBeanFactory().registerSingleton("customEventHandler", 
      new CustomEventHandler()); 

내가 ApplicationContext에 대한 참조를했습니다 ApplicationListeners

+0

에 이벤트를 게시하는 ApplicationEventMulticaster에 만약 ApplicationListener 구현을 추가합니다

context.addApplicationListener(new CustomEventHandler()); 

이 변경되어야한다. 이 빈과 애플리케이션 컨텍스트가 주어지면 코드를 통해 ApplicationEvent를 수신하는 방법이 있습니까? –

+0

작업 예제 및 비 작동 예제를 보여주는 코드를 게시하십시오. – UserF40

+0

작동하는 케이스와 작동하지 않는 케이스를 설명하는 샘플 코드를 게시했습니다. 도움을받을 수 있는지 확인하십시오. –

관련 문제