title: Spring 4 - Autowiring Issue: org.springframework.beans.factory.UnsatisfiedDependencyException: No qualifying bean of type
body: <p>Have a Spring MVC 4.0.3.RELEASE and (is not Spring Boot)...</p>
  
  <p>This is actually a follow up to a previous post where a member told me to redesign a Spring MVC Controller and DAO relationship:</p>
  
  <p><a href="https://stackoverflow.com/questions/44692318/spring-4-jdbc-how-to-load-db-properties-and-optimize-using-cache-or-db-connec">Spring 4 JDBC - How to load DB Properties and optimize (using Cache or DB Connection Pool)</a></p>
  
  <p>Something is obviously not right:</p>
  
  <p>pom.xml:</p>
  
  <pre><code>&lt;properties&gt;
      &lt;java-version&gt;1.7&lt;/java-version&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
      &lt;org-springframework-version&gt;4.0.3.RELEASE&lt;/org-springframework-version&gt;
  &lt;/properties&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
      &lt;version&gt;${org-springframework-version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
      &lt;version&gt;${org-springframework-version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
      &lt;version&gt;${org-springframework-version}&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
      &lt;version&gt;${org-springframework-version}&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>src/main/resources/sampledb.xml:</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"&gt;
  
      &lt;bean id="sampledb"  class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"&gt;
          &lt;property name="driverClassName"&gt;&lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;&lt;/property&gt;
          &lt;property name="url"&gt;&lt;value&gt;jdbc:mysql://localhost/mydatabase?zeroDateTimeBehavior=convertToNull&lt;/value&gt;&lt;/property&gt;
          &lt;property name="username"&gt;&lt;value&gt;root&lt;/value&gt;&lt;/property&gt;
          &lt;property name="password"&gt;&lt;value&gt;&lt;/value&gt;&lt;/property&gt;
          &lt;property name="maxIdle" value="10"/&gt;
          &lt;property name="maxActive" value="50"/&gt;
          &lt;property name="maxWait" value="100"/&gt; 
          &lt;property name="defaultAutoCommit" value="false"/&gt;
          &lt;property name="removeAbandoned" value="true"/&gt;
          &lt;property name="removeAbandonedTimeout" value="1"/&gt;
          &lt;property name="minIdle" value="0"&gt;&lt;/property&gt;
          &lt;property name="timeBetweenEvictionRunsMillis" value="1000"&gt;&lt;/property&gt;
          &lt;property name="minEvictableIdleTimeMillis" value="1000"&gt;&lt;/property&gt;
       &lt;/bean&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p>WEB-INF/web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app id="WebApp_ID" version="2.4" 
           xmlns="http://java.sun.com/xml/ns/j2ee" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
      &lt;display-name&gt;MyApp&lt;/display-name&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
          &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;/welcome-file-list&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;/web-app&gt;
  </code></pre>
  
  <p>WEB-INF/mvc-dispatcher-servlet.xml:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
  
      &lt;import resource="classpath:sampledb.xml" /&gt;
      &lt;context:component-scan base-package="com.myapp.rest.controllers" /&gt;
  
      &lt;mvc:annotation-driven /&gt;
  
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/pages/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>UserController:</p>
  
  <pre><code>package com.myapp.rest.controllers;
  
  @Controller
  @RequestMapping("/v2")
  public class UserController {
  
      private final UserDAO dao;
  
      @Autowired
      public AppUserController(UserDao dao) {
          this.dao = dao;
      }
  
      @RequestMapping(value="appUsers/{userId}",method=RequestMethod.GET)
      public @ResponseBody Object getUserDetails(@PathVariable String userId) {
          Object response=null;
          response=dao.getUser(userId);
          return response;
      }
  }
  </code></pre>
  
  <p>UserDao:</p>
  
  <pre><code>package com.myapp.rest.dao;
  
  @Repository
  @Service
  public class UserDAO {
  
      private final JdbcTemplate jdbcTemplate;
  
      @Autowired
      public UserDao(@Qualifier("sampledb") DataSource dataSource) {
          this.jdbcTemplate = new JdbcTemplate(dataSource);
      }
  
      // others methods which do the actual queries using Spring JDBC
  }
  </code></pre>
  
  <p>After copying the war into $CATALINA_HOME/webapps</p>
  
  <p>Tomcat starts and throws this exception into stdout:</p>
  
  <pre><code>SEVERE: Context initialization failed
      org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController' defined in file [/opt/tomcat/apache-tomcat-7.0.78/webapps/MyApp/WEB-INF/classes/com/myapp/rest/controllers/UserController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.myapp.rest.dao.UserDao]: : No qualifying bean of type [com.myapp.rest.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myapp.rest.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
          at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:747)
          at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
      Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myapp.rest.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
      Jun 23, 2017 12:16:51 AM org.apache.catalina.core.ApplicationContext log
      SEVERE: StandardWrapper.Throwable
      org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name 'userController' defined in file [/opt/tomcat/apache-tomcat-7.0.78/webapps/MyApp/WEB-INF/classes/com/myapp/rest/controllers/UserController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.myapp.rest.dao.UserDao]: : No qualifying bean of type [com.myapp.rest.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myapp.rest.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
          at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:747)
          at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
  
      Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myapp.rest.dao.UserDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
          ... 35 more
  
      Jun 23, 2017 12:16:51 AM org.apache.catalina.core.StandardContext loadOnStartup
      SEVERE: Servlet [mvc-dispatcher] in web application [/MyApp] threw load() exception
      org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myapp.rest.dao.UserDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
  </code></pre>