title: @Autowired bean not being found
body: <p>I'm getting errors trying to inject resource dependencies into my unit testing.
  My approach has been to write a TestConfig.java to replace the applicationContext.xml for production which manages the connections of the beans. So that I can run it with an in-memory database and just test components.</p>
  
  <p>TestConfig.java</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  public class TestConfig {
  @Bean
  public DataSource dataSource() {
      DriverManagerDataSource ds = new DriverManagerDataSource();
      ds.setDriverClassName("org.hsqldb.jdbcDriver");
      ds.setUrl("jdbc:hsqldb:mem:testdb");
      ds.setUsername("sa");
      ds.setPassword("");
      return ds;
  
  }
  
  @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){
  
      LocalContainerEntityManagerFactoryBean lcemfb
          = new LocalContainerEntityManagerFactoryBean();
  
      lcemfb.setDataSource(this.dataSource());
      lcemfb.setPackagesToScan(new String[] {"com.dao","com.data"});
      lcemfb.setPersistenceUnitName("MyTestPU");
  
      HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter();
      lcemfb.setJpaVendorAdapter(va);
  
      Properties ps = new Properties();
      ps.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
      ps.put("hibernate.hbm2ddl.auto", "create");
      lcemfb.setJpaProperties(ps);
  
      lcemfb.afterPropertiesSet();
  
      return lcemfb;
  
  }
  
  @Bean
  public PlatformTransactionManager transactionManager() {
      JpaTransactionManager tm = new JpaTransactionManager();
      tm.setEntityManagerFactory(this.entityManagerFactoryBean().getObject());
  
      return tm;
  }
  
  @Bean
  public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
      return new PersistenceExceptionTranslationPostProcessor();
  }
  
  @Bean
  public AutowiredAnnotationBeanPostProcessor autowiredAnnotationBeanPostProcessor()
  {
      return new AutowiredAnnotationBeanPostProcessor();
  }
  
  
  }
  </code></pre>
  
  <p>ProductsDaoTest.java</p>
  
  <pre><code>@ContextConfiguration(classes = { TestConfig.class })
  @RunWith(SpringJUnit4ClassRunner.class)
  public class ProductsDaoTest {
  
   @Resource(name="com.dao.ProductsDao")
   private ProductsDao testDao;
  
   @Test
   public void testSaveProduct() {
  
  
       Product productA = new Product();
       testDao.save(productA);
  
       Set&lt;Product&gt; products = testDao.getAllProducts();
  
       assertNotNull(products);
   }   
  }
  </code></pre>
  
  <p>The error is Error creating bean with name 'com.dao.ProductsDaoTest': Injection of resource dependencies failed </p>
  
  <p>So it can't find the ProductDao Bean which is a @Repository with a @Autowired sessionFactory. </p>
  
  <p>So my guess is that because I'm not naming the beans using xml it can't find it, though I thought it should automatically pick it up from setPackagesToScan(). So is there a way to manually insert the Bean mapping so that it can be found?</p>
  
  <p>Also more generally is this a reasonable way to go about testing Spring DAO configurations? </p>
  
  <p>Regards,
  Iain</p>