title: Spring: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
body: <p>I could not find a solution on this site to the following problem when I use <strong>Spring with annotations</strong> (not XML).</p>
  
  <p><strong>Any suggestion what I am doing wrong?</strong></p>
  
  <p>I have the following simple class. I do not extend it somewhere else.</p>
  
  <pre><code>@Component
  @Scope("prototype")
  public class Matryoshka
  {
      private Matryoshka[] matryoshki;
  
      public Matryoshka(Matryoshka[] matryoshki)
      {
          this.matryoshki = matryoshki;
      }
  }
  </code></pre>
  
  <p>Then I try to create instances of this class by Spring's <code>&lt;T&gt; T BeanFactory.getBean(Class&lt;T&gt; var1, Object... var2)</code></p>
  
  <p>From within a Spring scanned class I do something like:</p>
  
  <pre><code>Matryoshka matryoshka = new Matryoshka(new Matryoshka[] {});
  matryoshka = applicationContext.getBean(matryoshka.getClass(), new Matryoshka[] {matryoshka, matryoshka});
  //matryoshka = (Matryoshka) applicationContext.getBean("matryoshka", new Matryoshka[] {matryoshka, matryoshka}); //also gives same error
  </code></pre>
  
  <p>Error message is:</p>
  
  <p><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'matryoshka' defined in file [/home/...../Matryoshka.class]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)</code></p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>It looks like <code>BeanFactory.getBean(Class&lt;T&gt; var1, Object... var2)</code> works when the constructor of the bean I try to instantiate has a fixed number of parameters. E.g. </p>
  
  <p><code>public Matryoshka(Matryoshka matryoshki1, Matryoshka matryoshki2)</code>, </p>
  
  <p>but not <code>public Matryoshka(Matryoshka[] matryoshki)</code> </p>
  
  <p>or <code>public Matryoshka(Matryoshka... matryoshki)</code>.</p>
  
  <p>Is it possible to make Spring work with constructors with indefinite number of parameters?</p>
  
  <p>I have seen something similar (not a constructor though) in the following answer</p>
  
  <p><a href="https://stackoverflow.com/questions/39542043/a-function-i-want-to-spring-inject-takes-varargs-should-i-provide-an-overload-t">A function I want to Spring-inject takes varargs. Should I provide an overload that takes a list instead?</a></p>
  
  <p>but they used XML configuration.</p>