title: Spring MVC - How does @Autowired work?
body: <p>I am working on a web application using Java and Spring. I'm new at Spring, so as an opportunity to learn, I was basically given an existing application and was told to extend it.
  I am having trouble understanding how the @Autowired work. I understand the high-level concept of dependency injection, but I want to know how the @Autowired annotation knows which concrete implementation of an interface to inject?</p>
  
  <p>To put my question into context, I will explain the problem I am having:</p>
  
  <blockquote>
    <p>I have an <em>interface</em> called <strong>PostDao</strong>, and a <em>class</em> called <strong>PostDaoImpl</strong> which implements <strong>PostDao</strong>. I then have another <em>class</em> called <strong>PostDaoPublicImpl</strong> which extends <strong>PostDaoImpl</strong>. These classes exist in my persistence layer.</p>
    
    <p>I then have an <em>interface</em> called <strong>PostService</strong>, and a <em>class</em> called called <strong>PostServiceImpl</strong> which implement <strong>PostService</strong>. I then have another <em>class</em> called <strong>PostServicePublicImpl</strong> which extends <strong>PostServiceImpl</strong>. These classes exist in my services layer.</p>
  </blockquote>
  
  <p>In <strong>PostServiceImpl</strong> the following line injects the following object:</p>
  
  <pre><code>@Autowired private PostDao postDao; 
  //This injects an object of the class PostDaoImpl
  </code></pre>
  
  <p>My problem is, in <strong>PostServicePublicImpl</strong> how do I have the same declaration as above, but have it inject an object of the class <strong>PostDaoPublicImpl</strong>:</p>
  
  <pre><code>@Autowired private PostDao postDao; 
  //This injects an object of the class PostDaoPublicImpl
  </code></pre>
  
  <p>I feel as though if I understand how the @Autowired annotation works, then I will be able to solve this problem. Any help and suggestions to solve the problem would be greatly appreciated.</p>