title: Spring data mongodb: mongodb repositories not found when autowired
body: <p>I have the following stack:</p>
  
  <ul>
  <li>Spring MVC web app</li>
  <li>Spring Data MongoDB</li>
  </ul>
  
  <p>When I deploy the webapp and start server (Tomcat 7) I'am getting the following error :</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.qopuir.repository.UserRepository com.qopuir.service.UserService.userRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.qopuir.repository.UserRepository] 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.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1147)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
      at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
      at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
      at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
      at java.util.concurrent.FutureTask.run(FutureTask.java:138)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
      at java.lang.Thread.run(Thread.java:662)
  </code></pre>
  
  <p>I don't understand why Repositories are not found during autowiring of Services. Any idea?</p>
  
  <p>This is <strong>web.xml</strong> definition :</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;Spring Data MongoDB Tutorial&lt;/display-name&gt;
  
      &lt;!--
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath:spring/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      --&gt;
  
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>applicationContext.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:p="http://www.springframework.org/schema/p"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"&gt;
  
      &lt;context:property-placeholder properties-ref="deployProperties" /&gt;
  
      &lt;context:annotation-config /&gt;
      &lt;context:component-scan base-package="com.qopuir" /&gt;
  
      &lt;mvc:annotation-driven /&gt;
      &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt;
  
      &lt;import resource="trace-context.xml" /&gt;
      &lt;import resource="spring-data.xml" /&gt;
  
  
      &lt;bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean" p:location="/WEB-INF/spring.properties" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>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:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"&gt;
  
      &lt;!-- Declare a view resolver --&gt;
      &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="1" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>spring-data.xm</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:p="http://www.springframework.org/schema/p"
      xmlns:c="http://www.springframework.org/schema/c"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mongo="http://www.springframework.org/schema/data/mongo"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
          http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"&gt;
  
      &lt;context:property-placeholder properties-ref="deployProperties" /&gt;
  
      &lt;!-- MongoDB host --&gt;
      &lt;mongo:mongo host="${mongo.host.name}" port="${mongo.host.port}" /&gt;
  
      &lt;!-- Template for performing MongoDB operations --&gt;
      &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate" c:mongo-ref="mongo" c:databaseName="${mongo.db.name}" /&gt;
  
      &lt;!-- Activate Spring Data MongoDB repository support --&gt;
      &lt;mongo:repositories base-package="com.qoupir.repository" mongo-template-ref="mongoTemplate" /&gt;
  
      &lt;!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes --&gt;
      &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  
      &lt;!-- Service for initializing MongoDB with sample data using MongoTemplate --&gt;
      &lt;bean id="initMongoService" class="com.qopuir.service.InitMongoService" init-method="init" /&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>trace-context.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:p="http://www.springframework.org/schema/p"
      xmlns:aop="http://www.springframework.org/schema/aop"
      xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"&gt;
  
      &lt;!-- For parsing classes with @Aspect annotation --&gt;
      &lt;aop:aspectj-autoproxy /&gt;
  
      &lt;bean id="customizableTraceInterceptor" class="com.qopuir.aop.TraceInterceptor"
          p:enterMessage="Entering $[targetClassShortName].$[methodName]($[arguments])"
          p:exitMessage="Leaving $[targetClassShortName].$[methodName](): $[returnValue]" /&gt;
  
      &lt;aop:config&gt;
          &lt;aop:advisor advice-ref="customizableTraceInterceptor" pointcut="execution(public * com.qopuir.service..*(..))" /&gt;
          &lt;aop:advisor advice-ref="customizableTraceInterceptor" pointcut="execution(public * com.qopuir.controller..*(..))" /&gt;
      &lt;/aop:config&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>This is one of the repositories :</p>
  
  <pre><code>package com.qopuir.repository;
  
  import org.springframework.data.mongodb.repository.MongoRepository;
  
  import com.qopuir.domain.User;
  
  public interface UserRepository extends MongoRepository&lt;User, String&gt; {
      User findByUsername(String username);
  }
  </code></pre>
  
  <p>Here is the UserService :</p>
  
  <pre><code>package com.qopuir.service;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import com.qopuir.repository.RoleRepository;
  import com.qopuir.repository.UserRepository;
  
  @Service
  public class UserService {
      @Autowired
      private UserRepository userRepository;
  
      @Autowired
      private RoleRepository roleRepository;
  
      //... some methods
  }
  </code></pre>
  
  <p>I don't know if it is a problem of configuration or a problem with version of libraries (I am using the last ones)</p>
  
  <p><strong>pom.xml</strong> :</p>
  
  <pre><code>&lt;properties&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  
      &lt;!-- Spring --&gt;
      &lt;spring.core.version&gt;3.2.10.RELEASE&lt;/spring.core.version&gt;
  
      &lt;!-- AOP --&gt;        
      &lt;aspectj.version&gt;1.7.4&lt;/aspectj.version&gt;
  
      &lt;!-- Javax Servlet --&gt;
      &lt;tomcat.version&gt;7.0.52&lt;/tomcat.version&gt;
  
      &lt;!-- MongoDB --&gt;
      &lt;mongodb.driver.version&gt;2.12.1&lt;/mongodb.driver.version&gt;
      &lt;spring.data.mongodb.version&gt;1.5.2.RELEASE&lt;/spring.data.mongodb.version&gt;
  
      &lt;!-- Logging --&gt;        
      &lt;slf4j.version&gt;1.7.7&lt;/slf4j.version&gt;
  
      &lt;!-- CGLIB --&gt;
      &lt;cglib.version&gt;2.2.2&lt;/cglib.version&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;!-- Spring Core --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
          &lt;version&gt;${spring.core.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;${spring.core.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
          &lt;version&gt;${spring.core.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;${spring.core.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;${spring.core.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-oxm&lt;/artifactId&gt;
          &lt;version&gt;${spring.core.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-aspects&lt;/artifactId&gt;
          &lt;version&gt;${spring.core.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- AOP --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
          &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
          &lt;version&gt;${aspectj.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
          &lt;artifactId&gt;aspectjweaver&lt;/artifactId&gt;
          &lt;version&gt;${aspectj.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- CGLIB --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;cglib&lt;/groupId&gt;
          &lt;artifactId&gt;cglib-nodep&lt;/artifactId&gt;
          &lt;version&gt;${cglib.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Javax Servlet --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
          &lt;artifactId&gt;tomcat-servlet-api&lt;/artifactId&gt;
          &lt;version&gt;${tomcat.version}&lt;/version&gt;
          &lt;scope&gt;provided&lt;/scope&gt;
      &lt;/dependency&gt;
  
      &lt;!-- MongoDB --&gt;
      &lt;!-- MongoDB Java Driver --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
          &lt;artifactId&gt;mongo-java-driver&lt;/artifactId&gt;
          &lt;version&gt;${mongodb.driver.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;!-- Spring Data MongoDB --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
          &lt;artifactId&gt;spring-data-mongodb&lt;/artifactId&gt;
          &lt;version&gt;${spring.data.mongodb.version}&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;!-- Logging --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
          &lt;version&gt;${slf4j.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;jcl-over-slf4j&lt;/artifactId&gt;
          &lt;version&gt;${slf4j.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
          &lt;version&gt;${slf4j.version}&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>