title: Could not autowire field in spring. why?
body: <p>I keep getting this error, and can't figure out why.. yes I know there many people had similar issues, but reading the answers they got, does not solve my problem.</p>
  
  <blockquote>
    <p>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.service.ContactService net.controller.ContactController.contactService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [net.service.ContactService] 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)}</p>
  </blockquote>
  
  <p><strong>here is the controller:</strong></p>
  
  <pre><code>@Controller
  @SessionAttributes
  public class ContactController {
  
      @Autowired
      private ContactService contactService;
  //methods...
  
  
  }
  </code></pre>
  
  <p><strong>the ContactServiceImpl</strong></p>
  
  <pre><code>@Service("contactService")
  @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
  public class ContactServiceImpl implements ContactService {
  
      @Autowired
      private ContactDao contactDao;
  
      public ContactServiceImpl() {
      }
  
      @Override
      @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
      public void addContact(Contact contact) {
          contactDao.saveContact(contact);
      }
  
      @Override
      public List&lt;Contact&gt; getContacts() {
          return contactDao.getAllContacts();
      }
  
  }
  </code></pre>
  
  <p><strong>the ContactDaoImpl</strong></p>
  
  <pre><code>@Repository("contactDao")
  public class ContactDaoImpl implements ContactDao {
  
      @Autowired
      private SessionFactory sessionFactory;
  
      @Override
      public void saveContact(Contact contact) {
          sessionFactory.getCurrentSession().saveOrUpdate(contact);
      }
  
      @Override
      @SuppressWarnings("unchecked")
      public List&lt;Contact&gt; getAllContacts() {
          return (List&lt;Contact&gt;) sessionFactory.getCurrentSession().createQuery("from contact c").list();
      }
  
  }
  </code></pre>
  
  <p><strong>and the spring-servlet.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &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:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;
  
      &lt;context:property-placeholder location="classpath:jdbc.properties" /&gt;
      &lt;context:component-scan base-package="net.controller" /&gt;
  
  
      &lt;tx:annotation-driven transaction-manager="hibernateTransactionManager" /&gt;
  
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
          &lt;property name="viewClass"
              value="org.springframework.web.servlet.view.JstlView" /&gt;
          &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
          &lt;property name="driverClassName" value="${database.driver}" /&gt;
          &lt;property name="url" value="${database.url}" /&gt;
          &lt;property name="username" value="${database.user}" /&gt;
          &lt;property name="password" value="${database.password}" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;net.form.Contact&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
  
  
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="hibernateTransactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>