title: Eager instantiation via Guice TypeListener for Guava EventBus Subscribers
body: <p>I am using the <a href="https://github.com/google/guava/wiki/EventBusExplained" rel="nofollow">Guava EventBus</a> and I register the subscribers via a type listener based on a <a href="https://spin.atomicobject.com/2012/01/13/the-guava-eventbus-on-guice/" rel="nofollow">great example</a>. The current code looks like this</p>
  
  <pre><code>bindListener(methodsAnnotatedWith(Subscribe.class), new TypeListener() {
      @Override
      public &lt;I&gt; void hear(TypeLiteral&lt;I&gt; typeLiteral, TypeEncounter&lt;I&gt; typeEncounter) {
          typeEncounter.register(new InjectionListener&lt;I&gt;() {
              @Override
              public void afterInjection(I i) {
                  register(i); // register with event bus
              }
          });
      }
  });
  </code></pre>
  
  <p>In short the code does the following</p>
  
  <ol>
  <li>Find all classes that have a public method annotated with <a href="http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/eventbus/Subscribe.html" rel="nofollow"><code>@Subscribe</code></a></li>
  <li>After an injection, register the actual subscriber with the event bus</li>
  </ol>
  
  <p>The problem I have with this approach is that it only registers subscribers that are actually injected and have been initialized. If there is subscriber that has not been used prior to posting the event, it won't be initialized.</p>
  
  <p>All subscribers are correctly found in the <code>hear</code> method (before registering the injection listener on the type encounter), but they are not instantiated, because they are lazily initialized.</p>
  
  <p>Is there a way to do something like an eager instantiation (similar to <a href="https://google.github.io/guice/api-docs/latest/javadoc/com/google/inject/binder/ScopedBindingBuilder.html#asEagerSingleton--" rel="nofollow"><code>binding.asEagerSingleton()</code></a>) on the subscribers? I know I will have to take care of whether they are singletons.</p>