title: In spring make sure Guava Event Bus listener beans are instantiated before events are dispatched
body: <p>Say I have the following:</p>
  
  <p><strong>Config</strong></p>
  
  <p>Sets up the guava EventBus bean.</p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
      @Bean
      public EventBus eventBus() {
          return new EventBus(); // guava event bus
      }
  }
  </code></pre>
  
  <p><strong>Event listener</strong></p>
  
  <p>Listens and handles to some/all of the events that are put on the event bus.
  May have several different classes similar to this that subscribe to events in the application.</p>
  
  <pre><code>@Component
  public class EventListener {
  
      @Autowired
      public EventListener(EventBus eventBus) {
          eventBus.register(this); // register this instance with the event bus so it receives any events
      }
  
      @Subscribe
      public void onTestEvent(TestEvent e) {
          System.out.println("Caught event");
      }
  }
  </code></pre>
  
  <p><strong>Event Dispatcher</strong></p>
  
  <p>Something that puts events on the event bus.</p>
  
  <pre><code>@Component
  public class EventDispatcher {
      @Autowired
      public EventDispatcher(EventBus eventBus) {
          eventBus.post(new TestEvent());
      }
  }
  </code></pre>
  
  <p><strong>Main App Entry Point</strong></p>
  
  <pre><code>@ComponentScan
  @EnableAutoConfiguration
  public class Application {
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  }
  </code></pre>
  
  <p>How can I make sure that all of the classes that are listening to events are instantiated before the classes that post them? Ie at the moment it could be possible for the event dispatcher class to be instantiated and post the event before the listener is instantiated, meaning the listener would have missed the events.</p>
  
  <p>I was thinking maybe creating another annotation like @EventListener, and then have spring prioritise all classes with this annotation somehow, but not sure how to implement this or if this is actually a good idea anyway.</p>
  
  <p>Thanks.</p>