title: Required Multiple beans of same type in Spring
body: <p>A request before you mark it as duplicate. I have gone through the forum and couldn't find the solution for the problem anywhere.</p>
  <p>I am writing a code using Spring 3.2 and everything is purely annotation based. The code receives XML files which are derived form different XSD files.</p>
  <p>So we can say, there are five different XSD ( A1, A2, A3, A4, A5) and my code receives XML of any type, and I have the logic to identify the type of the XML upon arrival.</p>
  <p>Now, I am trying to un-marshal these using Spring OXM. But because there are multiple XSDs involved, we cannot actually using one Un-marshaller. So we need around five of them.</p>
  <p>In the <code>Configuration</code> class, I added five beans like below:</p>
  <pre><code>@Bean(name=&quot;A1Unmarshaller&quot;)
  public Jaxb2Marshaller A1Unmarshaller(){
      Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
      unMarshaller.setContextPath(&quot;package name for the classes generate by XSD A1&quot;);
  }
  
  @Bean(name=&quot;A2Unmarshaller&quot;)
  public Jaxb2Marshaller A2Unmarshaller(){
      Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
      unMarshaller.setContextPath(&quot;package name for the classes generate by XSD A2&quot;);
  }
  
  @Bean(name=&quot;A3Unmarshaller&quot;)
  public Jaxb2Marshaller A3Unmarshaller(){
      Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
      unMarshaller.setContextPath(&quot;package name for the classes generate by XSD A3&quot;);
  }
  
  @Bean(name=&quot;A4Unmarshaller&quot;)
  public Jaxb2Marshaller A4Unmarshaller(){
      Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
      unMarshaller.setContextPath(&quot;package name for the classes generate by XSD A4&quot;);
  }
  
  @Bean(name=&quot;A5Unmarshaller&quot;)
  public Jaxb2Marshaller A5Unmarshaller(){
      Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
      unMarshaller.setContextPath(&quot;package name for the classes generate by XSD A5&quot;);
  }
  </code></pre>
  <p>Now I have five different classes C1, C2, C3, C4 and C5 and I am trying to inject one unmarshaller bean into one class. That means <code>A1Unmarshaller</code> is autowired to <code>C1</code> and so on.</p>
  <p>When the Spring context is built, it throws an error saying it expected one bean of type <code>Jaxb2Marshaller</code> and got five.</p>
  <p><strong>Note</strong> It worked fine when done using XML configuration, so I am not sure if I am missing something. Please help.</p>
  <p><strong>EDIT</strong> The code for one of the classes C1 is below:</p>
  <pre><code>@Component
  public class C1{
  
  @Autowired
  private Jaxb2Marshaller A1Unmarshaller;
      A1 o = null
  
  public boolean handles(String event, int eventId) {
      if (null != event&amp;&amp; eventId == 5) {
                  A1 =  A1Unmarshaller.unMarshal(event);
          return true;
      }
      return false;
  }
  </code></pre>
  <p>}</p>