title: Please explain me this (ambiguity) behavior of autowiring through constructor in Spring
body: <p>I am providing small code snippet first, then I shall ask for the explanation.</p>
  
  <pre><code>public class A {
     private String msg;
     private B b;
  
     public A () {
        System.out.println("No argument constructor is called");
     }
  
     public A (B b) {
        System.out.println("Parameterized constructor is called");
           this.b = b;
     }
  
     // getters and setters
  }
  </code></pre>
  
  <p>==============================================</p>
  
  <pre><code>&lt;bean id="a" class="A" p:msg="hello" autowire="constructor"/&gt;
  
  &lt;bean id="b" class="B"/&gt;
  </code></pre>
  
  <p>==============================================</p>
  
  <p>OUTPUT: </p>
  
  <blockquote>
    <p>Parameterized constructor is called</p>
  </blockquote>
  
  <p>This is the normal behavior &amp; understandable</p>
  
  <h1>==============================================</h1>
  
  <p>Now, I am adding a new bean definition of class B, as given below.</p>
  
  <pre><code>&lt;bean id="a" class="A" p:msg="hello" autowire="constructor"/&gt;
  
  &lt;bean id="b" class="B"/&gt;
  
  &lt;bean id="c" class="B"/&gt;
  </code></pre>
  
  <p>So, as far I know, since autowiring through constructor internally uses 'byType', hence it will give an exception regarding bean uniqueness violation, e.g., this scenario occurs if I would have used autowire="byType".</p>
  
  <p>But strangely, the output is given below.</p>
  
  <p>OUTPUT: </p>
  
  <blockquote>
    <p>No argument constructor is called</p>
  </blockquote>
  
  <h1>========================================</h1>
  
  <p>However, please note that if there is no default constructor specified in class A, then the expected exception will occur. So, is it a default behavior of Spring IoC container? If yes, then kindly explain me this in detail.</p>
  
  <p>Thank you in advance.</p>
  
  <p>The question is also available in LinkedIn (<a href="http://www.linkedin.com/groups/Can-anyone-please-explain-me-46964.S.257960132?qid=b578f352-7e98-47d2-99ea-c257ad4dfede&amp;trk=group_most_popular_guest-0-b-ttl&amp;goback=.gmp_46964" rel="nofollow">Spring autowiring through constructor ambiguity</a>)</p>