title: How to register a Spring ApplicationListener implementation through code?
body: <p>I've an implementation of Spring <code>ApplicationListener</code>. It works fine and receives events when it's declared as a bean in context xml file or if I use <code>@Component</code> annotation.</p>
  
  <p>However, it doesn't receive events if I manually register it through code using the <code>ConfigurableListableBeanFactory</code>'s <code>registerSingleton()</code> method. </p>
  
  <p>I've added some sample code below that describes working and not working cases.</p>
  
  <p>CustomEvent.java</p>
  
  <pre><code>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";
      }
  }
  </code></pre>
  
  <p>CustomEventPublisher.java</p>
  
  <pre><code>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);
      }
  }
  </code></pre>
  
  <p>CustomEventHandler.java</p>
  
  <pre><code>package com.test.event;
  
  import org.springframework.context.ApplicationListener;
  
  public class CustomEventHandler 
     implements ApplicationListener&lt;CustomEvent&gt;{
  
     public void onApplicationEvent(CustomEvent event) {
        System.out.println(event.toString());
     }
  
  }
  </code></pre>
  
  <p>applicationContextWithListenerBean.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;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"&gt;
  
     &lt;bean id="customEventHandler" 
        class="com.test.event.CustomEventHandler"/&gt;
  
     &lt;bean id="customEventPublisher" 
        class="com.test.event.CustomEventPublisher"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>applicationContextWithoutListenerBean.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;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"&gt;
  
     &lt;bean id="customEventPublisher" 
        class="com.test.event.CustomEventPublisher"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>MainApp.java</p>
  
  <pre><code>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();
      }
  }
  </code></pre>
  
  <p>Is it not possible to register an <code>ApplicationListener</code> through code?</p>