title: @Autowired - No qualifying bean of type found for dependency
body: <p>I've started my project by creating entities, services and JUnit tests for services using Spring and Hibernate. All of this works great.
  Then I've added spring-mvc to make this web application using many different step-by-step tutorials, but when I'm trying to make Controller with <code>@Autowired</code> annotation, I'm getting errors from Glassfish during deployment. I guess that for some reason Spring doesn't see my services, but after many attempts I still can't handle it.</p>
  <p>Tests for services with</p>
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {&quot;classpath:/beans.xml&quot;})
  </code></pre>
  <p>and</p>
  <pre><code>@Autowired
  MailManager mailManager;
  </code></pre>
  <p>works properly.</p>
  <p>Controllers without <code>@Autowired</code> too, I can open my project in web browser without trouble.</p>
  <p>/src/main/resources/beans.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
         xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
         xmlns:jdbc=&quot;http://www.springframework.org/schema/jdbc&quot; xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
         xmlns:util=&quot;http://www.springframework.org/schema/util&quot;
         xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
          http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd&quot;&gt;
  
      &lt;context:property-placeholder location=&quot;jdbc.properties&quot; /&gt;
      
      &lt;context:component-scan base-package=&quot;pl.com.radzikowski.webmail&quot;&gt;
          &lt;context:exclude-filter type=&quot;annotation&quot; expression=&quot;org.springframework.stereotype.Controller&quot; /&gt;
      &lt;/context:component-scan&gt;
      
      &lt;!--&lt;context:component-scan base-package=&quot;pl.com.radzikowski.webmail.service&quot; /&gt;--&gt;
      
      &lt;bean id=&quot;dataSource&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot; destroy-method=&quot;close&quot;&gt;
          &lt;property name=&quot;driverClassName&quot; value=&quot;${jdbc.driverClassName}&quot; /&gt;
          &lt;property name=&quot;url&quot; value=&quot;${jdbc.url}&quot; /&gt;
          &lt;property name=&quot;username&quot; value=&quot;${jdbc.username}&quot; /&gt;
          &lt;property name=&quot;password&quot; value=&quot;${jdbc.password}&quot; /&gt;
      &lt;/bean&gt;
      
      &lt;!-- Persistance Unit Manager for persistance options managing --&gt;
      &lt;bean id=&quot;persistenceUnitManager&quot; class=&quot;org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager&quot;&gt;
          &lt;property name=&quot;defaultDataSource&quot; ref=&quot;dataSource&quot;/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Entity Manager Factory for creating/updating DB schema based on persistence files and entity classes --&gt;
      &lt;bean id=&quot;entityManagerFactory&quot; class=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt;
          &lt;property name=&quot;persistenceUnitManager&quot; ref=&quot;persistenceUnitManager&quot;/&gt;
          &lt;property name=&quot;persistenceUnitName&quot; value=&quot;WebMailPU&quot;/&gt;
      &lt;/bean&gt;
  
      &lt;!-- Hibernate Session Factory --&gt;
      &lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate4.LocalSessionFactoryBean&quot;&gt;
          &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&gt;
          &lt;!--&lt;property name=&quot;schemaUpdate&quot; value=&quot;true&quot; /&gt;--&gt;
          &lt;property name=&quot;packagesToScan&quot; value=&quot;pl.com.radzikowski.webmail.domain&quot; /&gt;
          &lt;property name=&quot;hibernateProperties&quot;&gt;
              &lt;props&gt;
                  &lt;prop key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      
      &lt;!-- Hibernate Transaction Manager --&gt;
      &lt;bean id=&quot;txManager&quot; class=&quot;org.springframework.orm.hibernate4.HibernateTransactionManager&quot;&gt;
          &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot;/&gt;
      &lt;/bean&gt;
      
      &lt;!-- Activates annotation based transaction management --&gt;
      &lt;tx:annotation-driven transaction-manager=&quot;txManager&quot;/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  <p>/webapp/WEB-INF/web.xml</p>
  <pre><code>&lt;web-app xmlns=&quot;http://java.sun.com/xml/ns/j2ee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; id=&quot;WebApp_ID&quot; version=&quot;2.4&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot;&gt;
      &lt;display-name&gt;Spring Web MVC Application&lt;/display-name&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;mvc-dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/mvc-dispatcher-servlet.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  &lt;/web-app&gt;
  </code></pre>
  <p>/webapp/WEB-INF/mvc-dispatcher-servlet.xml</p>
  <pre><code>&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
         xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
         xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd&quot;&gt;
      
      &lt;context:component-scan base-package=&quot;pl.com.radzikowski.webmail&quot; use-default-filters=&quot;false&quot;&gt;
          &lt;context:include-filter type=&quot;annotation&quot; expression=&quot;org.springframework.stereotype.Controller&quot; /&gt;
      &lt;/context:component-scan&gt;
      
      &lt;mvc:annotation-driven/&gt;
      
      &lt;bean class=&quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot;&gt;
          &lt;property name=&quot;prefix&quot; value=&quot;/WEB-INF/views/&quot; /&gt;
          &lt;property name=&quot;suffix&quot; value=&quot;.jsp&quot; /&gt;
      &lt;/bean&gt;
      
  &lt;/beans&gt;
  </code></pre>
  <p>pl.com.radzikowski.webmail.service.AbstractManager</p>
  <pre><code>package pl.com.radzikowski.webmail.service;
  
  import org.apache.log4j.Logger;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  
  /**
   * Master Manager class providing basic fields for services.
   * @author Maciej Radzikowski &lt;maciej@radzikowski.com.pl&gt;
   */
  public class AbstractManager {
  
      @Autowired
      protected SessionFactory sessionFactory;
  
      protected final Logger logger = Logger.getLogger(this.getClass());
  
  }
  </code></pre>
  <p>pl.com.radzikowski.webmail.service.MailManager</p>
  <pre><code>package pl.com.radzikowski.webmail.service;
  
  import org.springframework.stereotype.Component;
  import org.springframework.transaction.annotation.Transactional;
  
  @Component
  @Transactional
  public class MailManager extends AbstractManager {
      // some methods...
  }
  </code></pre>
  <p>pl.com.radzikowski.webmail.HomeController</p>
  <pre><code>package pl.com.radzikowski.webmail.controller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.ModelMap;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import pl.com.radzikowski.webmail.service.MailManager;
  
  @Controller
  @RequestMapping(&quot;/&quot;)
  public class HomeController {
  
      @Autowired
      public MailManager mailManager;
  
      @RequestMapping(value = &quot;/&quot;, method = RequestMethod.GET)
      public String homepage(ModelMap model) {
          return &quot;homepage&quot;;
      }
  
  }
  </code></pre>
  <p>Error:</p>
  <blockquote>
  <p>SEVERE:   Exception while loading the app<br />
  SEVERE:   Undeployment failed for context /WebMail<br />
  SEVERE:   Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public pl.com.radzikowski.webmail.service.MailManager pl.com.radzikowski.webmail.controller.HomeController.mailManager; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.com.radzikowski.webmail.service.MailManager] 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>Sorry for a lot of code, but I don't know what can cause that error anymore.</p>
  <p><strong>Added</strong></p>
  <p>I've created the interface:</p>
  <pre><code>@Component
  public interface IMailManager {
  </code></pre>
  <p>added implements:</p>
  <pre><code>@Component
  @Transactional
  public class MailManager extends AbstractManager implements IMailManager {
  </code></pre>
  <p>and changed autowired:</p>
  <pre><code>@Autowired
  public IMailManager mailManager;
  </code></pre>
  <p>But it still throws errors (also when I've tried with <code>@Qualifier</code>)</p>
  <blockquote>
  <p>..Could not autowire field: public
  pl.com.radzikowski.webmail.service.IMailManager
  pl.com.radzikowski.webmail.controller.HomeController.mailManager...</p>
  </blockquote>
  <p>I've tried with different combinations of <code>@Component</code> and <code>@Transactional</code> too.</p>
  <p>Shouldn't I include beans.xml in web.xml somehow?</p>