title: Spring-mybatis connection fails with UnsatisfiedDependencyException
body: <p>I am relatively new to Spring, and am having problem with establishing spring-mybatis setup environment. Here is my code. The exception I am getting is </p>
  
  <blockquote>
    <p>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController for com.infosender.service.UserServiceImpl.</p>
  </blockquote>
  
  <p>I don't get this issue. Because the UserServiceImpl object is autowired in LoginController and UserServiceImpl class is autowired to UserDaoMapper interface, there should be a bean named UserServiceImpl. Why could be the reason for the bean not found?</p>
  
  <pre><code>package com.infosender.controller;
  import java.sql.SQLException;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.ModelAttribute;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  import com.infosender.user.User;
  import com.infosender.user.UserDao;
  import com.infosender.user.UserDaoMapper;
  import com.infosender.service.UserService;
  import com.infosender.service.UserServiceImpl;;
  
  
  @Controller
  public class LoginController {
  
      //UserService userService = UserService.getInstance();
      @Autowired
      UserServiceImpl userServiceImpl;
  
      @RequestMapping(value="/login", method=RequestMethod.POST)
      public String handleRequestInternal(
              @ModelAttribute("username") String user_id,
              @ModelAttribute("password") String user_pw
              ) {
          User user = null;
          try {
              user = userServiceImpl.getUserByIdAndPw(user_id, user_pw);
          } catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
          if(user == null) {
              return "login_fail";
          }
          return "main_menu";     
      }
  }
  </code></pre>
  
  <p>And here is UserService and UserServiceImpl</p>
  
  <pre><code>package com.infosender.service;
  
  import java.sql.SQLException;    
  import javax.inject.Inject;    
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;    
  import com.infosender.user.User;
  import com.infosender.user.UserDao;    
  public interface UserService {
  
      public User getUserByIdAndPw(String user_id, String user_pw)throws SQLException;
  }
  
  package com.infosender.service;
  import java.sql.SQLException;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Service;
  
  import com.infosender.user.User;
  import com.infosender.user.UserDao;
  import com.infosender.user.UserDaoMapper;
  
  @Service
  public class UserServiceImpl implements UserService {
  
      @Autowired
      private UserDaoMapper userDaoMapper;    
  
      public User getUserByIdAndPw(String user_id, String user_pw)throws SQLException {
          return userDaoMapper.getUserByIdAndPw(user_id, user_pw);
      }   
  }
  </code></pre>
  
  <p>Here I have UserDao and UserDaoMapper</p>
  
  <pre><code>package com.infosender.user;
  
  import java.sql.SQLException;
  import org.apache.ibatis.session.SqlSession;
  import org.apache.ibatis.session.SqlSessionFactory;
  import org.mybatis.spring.SqlSessionFactoryBean;
  import org.mybatis.spring.SqlSessionTemplate;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Controller;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public class UserDao implements UserDaoMapper{
  
      @Autowired  
      private SqlSessionTemplate sqlSession;
  
  
      public void setSqlSessionTemplate(SqlSessionTemplate sqlSession) {
          this.sqlSession = sqlSession;
      }
  
      public User getUserByIdAndPw(String user_id, String user_password) {
          UserDaoMapper mapper = sqlSession.getMapper(UserDaoMapper.class);
          return mapper.getUserByIdAndPw(user_id, user_password);
  
          //return sqlSession.selectOne("com.infosender.user.UserDaoMapper.getUserByIdAndPw");        
      }   
  }
  
  package com.infosender.user;
  import org.apache.ibatis.annotations.Param;
  import org.springframework.stereotype.Repository;
  
  public interface UserDaoMapper {
      public User getUserByIdAndPw(@Param("user_id")String user_id,@Param("user_password")String user_password);  
  }
  </code></pre>
  
  <p>And the below is root-context.xml configuration.</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:jdbc="http://www.springframework.org/schema/jdbc"
      xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
      xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
          http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"&gt;
  
      &lt;context:annotation-config/&gt;
  
  
  
      &lt;!-- org.apache.commons.dbcp.BasicDataSource --&gt;
      &lt;!-- Root Context: defines shared resources visible to all other web components --&gt;
      &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;  
          &lt;property name="driverClassName" value="com.mysql.jdbc.Driver"&gt;&lt;/property&gt;
          &lt;property name="url" value="jdbc:mysql://127.0.0.1:3306/InfoSender"&gt;&lt;/property&gt;
          &lt;property name="username" value="root"&gt;&lt;/property&gt;
          &lt;property name="password" value="root"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
          &lt;property name="configLocation" value="classpath:/WEB_INF/spring/mybatis/mybatis-config.xml"&gt;&lt;/property&gt;
          &lt;property name="mapperLocations" value="classpath:*/mappers/*mapper.xml"&gt;&lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"&gt;
          &lt;constructor-arg index="0" ref="sqlSessionFactory"&gt;&lt;/constructor-arg&gt;
      &lt;/bean&gt;
  
  &lt;!--    &lt;bean id="userDaoMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"&gt;
          &lt;property name="mapperInterface" value="com.infosender.user.UserDaoMapper"&gt;&lt;/property&gt;
          &lt;property name="sqlSessionFactory" ref="sqlSessionFactory"&gt;&lt;/property&gt;
  
      &lt;/bean&gt;
       --&gt;
  
      &lt;context:component-scan base-package="com.infosender.*" /&gt;
  
      &lt;/beans&gt;
  </code></pre>
  
  <p>Here is the entire error log</p>
  
  <pre><code>ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.infosender.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
      at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
      at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:696)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:662)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:710)
      at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:587)
      at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:526)
      at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:169)
      at javax.servlet.GenericServlet.init(GenericServlet.java:158)
      at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1238)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1151)
      at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1038)
      at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5337)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
      at java.util.concurrent.FutureTask.run(FutureTask.java:266)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
      at java.lang.Thread.run(Thread.java:748)
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.infosender.service.UserService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1646)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1205)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1166)
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593)
      ... 31 more
  </code></pre>