title: Spring Autowired DAO with hibernate.cfg.xml
body: <p>I have a Spring Maven DAO Project which is packaged into a jar.  It was originally a Spring DAO Ivy project, but I converted it to maven.   This was a legacy project which used Spring 3.2.0 and Hibernate 3.5, and now I created a new project and updated to Spring 3.2.3.RELEASE and Hibernate 4.2.3.Final.   I do try to make sure all my jars were updated.</p>
  
  <p>As it stands, the app builds with maven, and all the java code seems to compile.</p>
  
  <p>This DAO does not use annoted entities, instead it uses a legacy hibernate.cfg.xml file, and a lot of hbm.xml files ... which I hope I can use.   I do not relish the idea of converting the very many entities to annoted entities, and then converting the DAO code.</p>
  
  <p>In any event, with the configuration below, I want to test loading a single DAO to test out the configuration.  But, I get an error message as listed below.   I spent a day researching this on google and here to see if anyone had the exact error.</p>
  
  <p>If I had build a new project from scratch, I would have annoted entities, but I don't have that right now, so I have to tweak the configuration to get this DAO project to work correctly.</p>
  
  <p>here is the error:</p>
  
  <pre><code>2013-08-06 13:36:42,834 INFO [org.springframework.context.support.GenericApplicationContext] - &lt;Refreshing org.springframework.context.support.GenericApplicationContext@165973ea: startup date [Tue Aug 06 13:36:42 EDT 2013]; root of context hierarchy&gt;
  2013-08-06 13:36:42,881 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] -  &lt;Pre-instantiating singletons in  org.springframework.beans.factory.support.DefaultListableBeanFactory@6c4fc156: defining beans     [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy&gt;
  2013-08-06 13:36:42,912 ERROR [org.springframework.test.context.TestContextManager]     
  &lt;Caught exception while allowing TestExecutionListener     [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@667cbde6] to prepare test instance [com.tom.project.dao.AaaaBaseDAOLoaderTest@7977b9b]&gt;
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.tom.project.dao.AaaaBaseDAOLoaderTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
  Could not autowire field: public com.tom.project.dao.accounting.AccountingAccountDAO  com.tom.project.dao.AaaaBaseDAOLoaderTest.accountingAccountDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.tom.project.dao.accounting.AccountingAccountDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: 
  {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
  at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
  at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
  at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
  at  org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
  at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
  at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
  at  org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.tom.projct.dao.accounting.AccountingAccountDAO com.tom.project.dao.AaaaBaseDAOLoaderTest.accountingAccountDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.tom.project.dao.accounting.AccountingAccountDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
  at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
  ... 26 more
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.tom.project.dao.accounting.AccountingAccountDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  at     org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:986)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:856)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)
  at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
  ... 28 more
  2013-08-06 13:36:42,928 INFO [org.springframework.context.support.GenericApplicationContext] - &lt;Closing org.springframework.context.support.GenericApplicationContext@165973ea: startup date [Tue Aug 06 13:36:42 EDT 2013]; root of context hierarchy&gt;
  2013-08-06 13:36:42,928 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - &lt;Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6c4fc156: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy&gt;
  </code></pre>
  
  <p>So, here is the applicationContext.xml:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:lang="http://www.springframework.org/schema/lang"
  xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"&gt;
  
  &lt;!-- Hibernate SessionFactory --&gt;
  &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean"&gt;
      &lt;property name="dataSource"&gt;
          &lt;ref bean="pooledDataSource" /&gt;
      &lt;/property&gt;
      &lt;property name="configLocation"&gt;
          &lt;value&gt;classpath*:hibernate.cfg.xml&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/&gt;
  &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/&gt;
  
  &lt;!-- Activates scanning of @Autowired --&gt;
  &lt;context:annotation-config/&gt;
  
  &lt;!-- Activates scanning of @Repository and @Service --&gt;
  &lt;context:component-scan base-package="com.tom.project"/&gt;
  
  &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;  
  
  &lt;!-- enable the configuration of transactional behavior based on annotations --&gt;
  &lt;tx:annotation-driven/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>and here is the project-infrastructure.xml file:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:lang="http://www.springframework.org/schema/lang"
  xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"&gt;
  
  &lt;!--  Properties files --&gt;
  &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="locations"&gt;
          &lt;list&gt;
              &lt;value&gt;classpath*:hibernate.properties&lt;/value&gt;
              &lt;value&gt;classpath*:project.properties&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!-- Database Connection Pool --&gt;
  &lt;bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt;
      &lt;property name="idleConnectionTestPeriod" value="100800"/&gt;
      &lt;property name="maxIdleTime" value="201600"/&gt;
      &lt;property name="driverClass"&gt;
          &lt;value&gt;${project.hibernate.connection.driver_class}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="jdbcUrl"&gt;
          &lt;value&gt;${project.hibernate.connection.url}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="user"&gt;
          &lt;value&gt;${project.hibernate.connection.username}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="password"&gt;
          &lt;value&gt;${project.hibernate.connection.password}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="maxPoolSize"&gt;
          &lt;value&gt;1000&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;!--  Transaction Manager --&gt;
  &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Here is the interface for my DAO:</p>
  
  <pre><code>public interface AccountingAccountDAO {
      void delete(AccountingAccount accountingAccountID);
      void persist(AccountingAccount accountingAccountID, User queryUser);
      List&lt;AccountingAccount&gt; getAllByAttribute(String fieldName, String name);
      AccountingAccount getById(int i);
  }
  </code></pre>
  
  <p>The implementation of the DAO:</p>
  
  <pre><code>@Repository("accountingAccountDAO")
  public class HibernateAccountingAccountDAO implements AccountingAccountDAO
  {
      @Autowired
      private SessionFactory sessionFactory;
  
      public SessionFactory getSessionFactory()
      {
          return sessionFactory;
      }
  
      public void setSessionFactory(SessionFactory sessionFactory)
      {
          this.sessionFactory = sessionFactory;
      }
  
      private static final Log logger = LogFactory.getLog(HibernateAccountingAccountDAO.class);
  
      @Override
      public void delete(AccountingAccount accountingAccountID)
       {
       }
  
      @Override
      public void persist(AccountingAccount accountingAccountID, User query2User)
      {
      }
  
      @Override
      public List&lt;AccountingAccount&gt; getAllByAttribute(String fieldName, String name)
      {
          return null;
       }
  
       @Override
       public AccountingAccount getById(int i)
       {
           return null;
       }
  
  }
  </code></pre>
  
  <p>And here is the unit test which generates the message:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations =
  {    "classpath*:project-infrastructure.xml","classpath*:applicationContext.xml"})
  public class AaaaBaseDAOLoaderTest
  {
      @Autowired
      public AccountingAccountDAO accountingAccountDAO;
  
      public AccountingAccountDAO getAccountingAccountDAO()
      {
          return accountingAccountDAO;
      }
  
      public void setAccountingAccountDAO(AccountingAccountDAO accountingAccountDAO)
      {
          this.accountingAccountDAO = accountingAccountDAO;
      }
  
      @Test
      public void initResources()
      {
          accountingAccountDAO.getClass();
      }
  }
  </code></pre>