title: Unable to catch and handle spring custom events in java
body: <p>I am trying to set up a custom spring event publisher and listener. 
  The flow of the process seems to be working fine, I can see the prints before the event publishes,  however, my event listeners do not catch the event.
  my main class is:</p>
  
  <pre><code>public class TestClass {
  
          public static void main(String[] args) throws Exception {
              testEvents();
          }
  
          private static void testEvents() throws InterruptedException {
              AnnotationConfigApplicationContext eventPublisherContext = new AnnotationConfigApplicationContext(EventListenerConfig.class);
              CustomSpringEventPublisher eventPublisher = eventPublisherContext.getBean(CustomSpringEventPublisher.class);
              List&lt;CustomSpringEventListener&gt; listenersList = new ArrayList&lt;CustomSpringEventListener&gt;();
  
              for (int i = 0; i &lt; 5; i++) {
                  listenersList.add(new CustomSpringEventListener("LR" + (i + 1)));
              }
  
              for (int i = 0; i &lt; 10; i++) {
                  Thread.sleep(5000);
  
                  LocalDateTime now  = LocalDateTime.now();
                  eventPublisher.doStuffAndPublishAnEvent("new event " + now.format(Formats.isolong));
              }
          }
      }
  </code></pre>
  
  <p>here is my custom event class:</p>
  
  <pre><code>public class CustomSpringEvent extends ApplicationEvent {
      private String message;
  
      public CustomSpringEvent(Object source, String message) {
          super(source);
          this.message = message;
      }
  
      public String getMessage() {
          return message;
      }
  
  }
  </code></pre>
  
  <p>here is my custom event publisher class:</p>
  
  <pre><code>@Component
  public class CustomSpringEventPublisher {
  
      @Autowired
      private ApplicationEventPublisher applicationEventPublisher;
  
      public void doStuffAndPublishAnEvent(final String message) {
          System.out.println("Publishing custom event --&gt; " + message);
          this.applicationEventPublisher.publishEvent(new CustomSpringEvent(this, message));
      }
  
  }
  </code></pre>
  
  <p>here is my custom event listener class:</p>
  
  <pre><code>public class CustomSpringEventListener implements ApplicationListener&lt;CustomSpringEvent&gt; {
      private String name;
  
      public CustomSpringEventListener(String name) {
          this.name = name;
      }
  
      @Override
      @EventListener
      public void onApplicationEvent(CustomSpringEvent event) {
          System.out.println(name + " received spring custom event - " + event.getMessage());
      }
  
  }
  </code></pre>
  
  <p>here is my event listener config class:</p>
  
  <pre><code>@Configuration
  @ComponentScan({"some.local.directory.testCases"})
  public class EventListenerConfig {
  }
  </code></pre>
  
  <p>What am I missing here? How can I get my listener class to recognize the published event and handle it?
  I saw some examples in which the listener class has the <code>@Component</code>, but when I tried to add it I got an error from spring:
  <code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate.</code>, also I don't see any reason why would the listener class have this annotation in this case.
  Any help would be appreciated. Thank you.</p>