title: Spring3 mybatis 3 mapper no such bean found exception
body: <p>I am trying Spring 3 + Mybatis3 + mysql but didn't work out, tried almost all of the tutorials getting same exception, may be some one here can help me out</p>
  
  <p>Following is the Error trace output and i have also attached list down the source code please help me out</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.triage.persistance.UserService com.triage.persistance.UserServiceImpl.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.triage.persistance.UserService] 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:506)
          at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
          ... 39 more
      Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.triage.persistance.UserService] 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:924)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
          at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
          ... 41 more
  </code></pre>
  
  <p>** User.java ** </p>
  
  <pre><code>package com.triage.domain;
  
  public class User {
  
      private long id;
      private String name;
      private String standard;
      private int age;
      private String sex;
      public long getId() {
          return id;
      }
      public void setId(long id) {
          this.id = id;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public String getStandard() {
          return standard;
      }
      public void setStandard(String standard) {
          this.standard = standard;
      }
      public int getAge() {
          return age;
      }
      public void setAge(int age) {
          this.age = age;
      }
      public String getSex() {
          return sex;
      }
      public void setSex(String sex) {
          this.sex = sex;
      }
  }
  </code></pre>
  
  <p>** UserService.xml**</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt;
  
  &lt;mapper namespace="com.triage.persistance.UserService"&gt;
  
      &lt;resultMap id="userResult" type="user"&gt;
          &lt;result property="id" column="id" /&gt;
          &lt;result property="name" column="name" /&gt;
          &lt;result property="standard" column="standard" /&gt;
          &lt;result property="age" column="age" /&gt;
          &lt;result property="sex" column="sex" /&gt;
  
      &lt;/resultMap&gt;
  
      &lt;select id="getAllUsers" resultMap="userResult"&gt;
          SELECT id, name, standard, age, sex FROM USER
      &lt;/select&gt;
  &lt;/mapper&gt;
  </code></pre>
  
  <p>** sqlmap-config.xml**</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd"&gt;
  
  &lt;configuration&gt;
      &lt;settings&gt;
          &lt;!-- changes from the defaults --&gt;
         &lt;setting name="lazyLoadingEnabled" value="false" /&gt;
      &lt;/settings&gt;
      &lt;typeAliases&gt;
          &lt;typeAlias type="com.triage.domain.User" alias="user"/&gt;
      &lt;/typeAliases&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>** UserService.java **</p>
  
  <pre><code>package com.triage.persistance;
  
  import java.util.List;
  
  import com.triage.domain.User;
  
  public interface UserService {
  
      public List&lt;User&gt; getAllUsers();
  }
  </code></pre>
  
  <p>** test-application context **</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:p="http://www.springframework.org/schema/p"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
      xmlns:jee="http://www.springframework.org/schema/jee" 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/util
          http://www.springframework.org/schema/util/spring-util-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/jee
          http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          "&gt;
  
      &lt;aop:aspectj-autoproxy /&gt;
  
      &lt;context:component-scan base-package="com.triage" /&gt;
  
      &lt;context:property-placeholder location="classpath:properties/jdbc.properties" /&gt;
  
  
      &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
          destroy-method="close"&gt;
          &lt;property name="driverClass" value="${jdbc.driverClassName}" /&gt;
          &lt;property name="jdbcUrl" value="${jdbc.url}" /&gt;
          &lt;property name="user" value="${jdbc.username}" /&gt;
          &lt;property name="password" value="${jdbc.password}" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="txManager" /&gt;
  
      &lt;bean id="txManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
      &lt;!-- jdbc template --&gt;
      &lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"&gt;
          &lt;property name="dataSource"&gt;
              &lt;ref bean="dataSource" /&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 name="configLocation" value="classpath:sqlmap-config.xml" /&gt; 
          &lt;property name="mapperLocations"
              value="classpath:mappers/*.xml" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>** My test case  Execution point **</p>
  
  <pre><code>package com.triage.test;
  import java.util.List;
  
  import org.junit.Assert;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.TestExecutionListeners;
  import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
  
  import com.triage.domain.User;
  import com.triage.persistance.UserService;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = "classpath:test-applicationContext.xml")
  @TestExecutionListeners(value = { DependencyInjectionTestExecutionListener.class })
  public class HelloPortletTestContext extends AbstractJUnit4SpringContextTests {
  
      @Autowired
      private UserService userService; ** Error no such bean found error
  //
  //  @Autowired
  //  private SqlSessionFactory sqlSessionFactory; // If i try this successfully found this object
  
      @Test
      public void testHelloAccount() {
          List&lt;User&gt; items = userService.getAllUsers();
          System.out.println(items.size());
          Assert.assertTrue(!items.isEmpty());
          Assert.assertTrue(true);
      }
  
  
  }
  </code></pre>