title: Spring MVC - Bean not available with @Autowired
body: <p>I'm having some trouble getting a data source bean, defined in an xml file, into a class in Spring using @Autowired annotation.</p>
  
  <p>Would anyone have an idea what's happening here? It seems as though dataSource in the *Impl class is not being wired in.</p>
  
  <p>dao-context.xml</p>
  
  <pre><code>&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
      &lt;property name="url" value="jdbc:mysql://localhost:8889/spring" /&gt;
      &lt;property name="username" value="user" /&gt;
      &lt;property name="password" value="1234" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>PersonDaoImpl.java</p>
  
  <pre><code>public class PersonDaoImpl implements PersonDao {
  
  @Autowired
  private DataSource dataSource;  
  private JdbcTemplate jdbcTemplate = new JdbcTemplate();
  
  
  public DataSource getDataSource() {
      return dataSource;
  }
  
  public void setDataSource(DataSource dataSource) {
      System.out.println("SETTING DATA SOURCE");
      this.dataSource = dataSource;
  }
  
  public List&lt;Person&gt; getPersonList() {
      // TODO Auto-generated method stub
      return null;
  }
  
  public void savePerson(Person person) {
      // TODO Auto-generated method stub
  
  }
  
  public Person getPersonById(int id) {
      System.out.println("Getting Person: " + id);        
      return null;
  }
  
  @Override
  public int getPersonCount(){
      String sql = "SELCECT COUNT(*) FROM PERSON";
      jdbcTemplate.setDataSource(getDataSource());
      int count = jdbcTemplate.queryForInt(sql);
      System.out.println("Count of Person: " + count);
      return count;
  }
  
  public JdbcTemplate getJdbcTemplate() {
      return jdbcTemplate;
  }
  
  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
      this.jdbcTemplate = jdbcTemplate;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>...and finally, the exception.</p>
  
  <pre><code>org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No DataSource specified
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  
  java.lang.IllegalArgumentException: No DataSource specified
      org.springframework.util.Assert.notNull(Assert.java:112)
      org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:97)
      org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
      org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:382)
      org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
      org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:464)
      org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:472)
      org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:477)
      org.springframework.jdbc.core.JdbcTemplate.queryForInt(JdbcTemplate.java:486)
      com.myPackage.rest.dao.impl.PersonDaoImpl.getPersonCount(PersonDaoImpl.java:50)
      com.myPackage.rest.HomeController.home(HomeController.java:36)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:597)
      org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
      org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
      org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
      org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
      org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
      org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
      org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
      org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
      org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  </code></pre>
  
  <p>Any help would be greatly appreciated, cheers.</p>