title: Spring generic bean definition singleton per generic type
body: <p>Is it possible to define a generic bean <em>once</em> like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Configuration
  public class TestConfiguration {
  
      @Bean
      public &lt;T&gt; EmitterProcessor&lt;T&gt; emitterProcessor() {
          return EmitterProcessor.create();
      }
  }
  </code></pre>
  
  <p>and then have a unique instance per parameterized type <code>T</code> so that the following test passes:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(classes = TestConfiguration.class)
  public class GenericBeanTest {
  
      @Autowired
      private EmitterProcessor&lt;String&gt; stringEmitterProcessor;
  
      @Autowired
      private EmitterProcessor&lt;Integer&gt; integerEmitterProcessor;
  
      @Test
      public void testExistsAndIsUnique() {
          assertNotNull(stringEmitterProcessor);
          assertNotNull(integerEmitterProcessor);
          assertNotSame(stringEmitterProcessor, integerEmitterProcessor);
      }
  }
  
  </code></pre>
  
  <p>Currently the <code>stringEmitterProcessor</code> and <code>integerEmitterProcessor</code> are the same object so that the last line in the test <code>assertNotSame(stringEmitterProcessor, integerEmitterProcessor);</code> fails.</p>
  
  <p>The only other way that I could imagine requires me to implement a custom <code>BeanFactory</code> that would make a distinction based on the parameterized type when autowiring by type.</p>