title: Spring bean created but not autowired
body: <p>I am trying to run TestNG tests for DAO class in Spring. But DataSource reference is not autowired despite of the annotation. Here is part of the test:</p>
  
  <pre><code>@ContextConfiguration(locations={"classpath:WEB-INF/servlet-context.xml"})
  public class ToDoDaoImplTest extends AbstractTestNGSpringContextTests {
  
      @Autowired
      // Construction of this object fails
      private ToDoItemDaoImpl toDoDao;
  }
  </code></pre>
  
  <p>Here is my Spring configuration:</p>
  
  <pre><code>&lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/view/"/&gt;
      &lt;property name="suffix" value=".jsp"/&gt;
  &lt;/bean&gt;
  
  &lt;!-- DataSource to be injected --&gt;
  &lt;bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="org.h2.Driver" /&gt;
      &lt;property name="url" value="jdbc:h2:mem:test" /&gt;
      &lt;property name="username" value="sa" /&gt;
      &lt;property name="password" value="" /&gt;
  &lt;/bean&gt;
  
  &lt;jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS"&gt;
      &lt;jdbc:script location="classpath:db.create.sql" /&gt;
  &lt;/jdbc:initialize-database&gt;
  
  &lt;context:component-scan base-package="org.myapp"/&gt;
  </code></pre>
  
  <p>DAO class:</p>
  
  <pre><code>@Repository
  public class ToDoItemDaoImpl implements ToDoItemDao {
      private DataSource dataSource;
      private SimpleJdbcInsert insert;
  
      public ToDoItemDaoImpl() {
          // dataSource is null here
          insert = new SimpleJdbcInsert(dataSource).withTableName("toDoItem").usingGeneratedKeyColumns("id");
      }
  
      @Autowired
      @Qualifier("dataSource")
      // This method is not called by Spring
      public void setDataSource(DataSource dataSource) {
          this.dataSource = dataSource;
      }
  </code></pre>
  
  <p>Here is stacktrace of the error:</p>
  
  <pre><code>Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.myapp.dao.ToDoItemDaoImpl]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required
      at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
      at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1004)
      ... 50 more
  Caused by: java.lang.IllegalArgumentException: Property 'dataSource' is required
      at org.springframework.jdbc.support.JdbcAccessor.afterPropertiesSet(JdbcAccessor.java:134)
      at org.springframework.jdbc.core.JdbcTemplate.&lt;init&gt;(JdbcTemplate.java:165)
      at org.springframework.jdbc.core.simple.AbstractJdbcInsert.&lt;init&gt;(AbstractJdbcInsert.java:97)
      at org.springframework.jdbc.core.simple.SimpleJdbcInsert.&lt;init&gt;(SimpleJdbcInsert.java:60)
      at org.myapp.dao.ToDoItemDaoImpl.&lt;init&gt;(ToDoItemDaoImpl.java:33)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
      at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
      ... 52 more
  null
  </code></pre>
  
  <p>Part of the log that shows that dataSource is created:</p>
  
  <pre><code>DEBUG - DefaultListableBeanFactory - Creating shared instance of singleton bean 'dataSource'
  DEBUG - DefaultListableBeanFactory - Creating instance of bean 'dataSource'
  DEBUG - DefaultListableBeanFactory - Eagerly caching bean 'dataSource' to allow for resolving potential circular references
  INFO  - DriverManagerDataSource    - Loaded JDBC driver: org.h2.Driver
  DEBUG - DefaultListableBeanFactory - Finished creating instance of bean 'dataSource'
  DEBUG - DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.jdbc.datasource.init.DataSourceInitializer#0'
  DEBUG - DefaultListableBeanFactory - Creating instance of bean 'org.springframework.jdbc.datasource.init.DataSourceInitializer#0'
  DEBUG - DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.jdbc.datasource.init.DataSourceInitializer#0' to allow for resolving potential circular references
  DEBUG - DefaultListableBeanFactory - Returning cached instance of singleton bean 'dataSource'
  </code></pre>
  
  <p>This is confuses me. The dataSource object is created, but is not Autowired into a Spring-managed object. What am I doing wrong?</p>