title: spring - how to autowire data source?
body: <p>I'm having some problem with autowire and DI in general, so I hope that someone can help cause I've been stuck for days now. </p>
  
  <p>This is the code: </p>
  
  <pre><code>@Service
  public class TicketsController implements Controller {
    private TicketManager ticketManager;
  
    @Autowired
  public void setTicketManager(TicketManager ticketManager) {
      this.ticketManager = ticketManager;
  }
  ...
  }
  
  
  @Service
  public class SimpleTicketManager implements TicketManager {
    private TicketsDao ticketsDao;
  
  @Autowired
  public void setTicketsDao(TicketsDao ticketsDao) {
      this.ticketsDao = ticketsDao;
  }
   ...
  }
  
  @Repository
  public class JdbcTicketDao implements TicketsDao  {
    private DataSource dataSource;
    @Autowired
    public void setDataSource(DataSource dataSource)  {
      this.dataSource=dataSource;
        this.jdbcTemplate = new JdbcTemplate(this.dataSource);   
       }
  ...
  }
  
  public final class AppContext {
  ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  BeanFactory factory = context;
  TicketsController ticketsController = (TicketsController) factory.getBean("ticketsController");
  }
  ...
  }
  </code></pre>
  
  <p>In my beans.xml I've got: </p>
  
  <pre><code>&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClass" value="com.mysql.jdbc.Driver"/&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:3306/mytckdb"/&gt;
      &lt;property name="username" value="user"/&gt;
      &lt;property name="password" value="pass"/&gt;
  &lt;/bean&gt;
  &lt;context:component-scan base-package="bp.dao" /&gt;
  &lt;context:component-scan base-package="bp.mvc" /&gt;
  &lt;context:component-scan base-package="bp.svc" /&gt;
  &lt;context:component-scan base-package="bp.view" /&gt;
  </code></pre>
  
  <p>This doesn't work and I get: </p>
  
  <pre><code>Error creating bean with name 'jdbcTicketDao': Injection of autowired dependencies failed
  ... nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
      No matching bean of type [javax.sql.DataSource] found for dependency.` 
  </code></pre>
  
  <p>Can someone please help out with this?  What am I doing wrong? It seems that autowiring is working all until the next step where it fails when injecting dataSource.</p>
  
  <p><b>EDIT</b>: I was playing with the code, and forgot @Autowire before setDataSource() but it is supposed to be there. </p>