title: MongoDB Spring - No bean named 'mongoTemplate' is defined
body: <p>This is probably a common question regarding using Mongodb and Spring. I didn't expected to be asking here questions, because every time when I got stuck I could find the solutions here. But this time none of the already answered questions regarding this topic could help me to solve the problem...</p>
  
  <p>I have seriously no clue what to do... Here is the situation: </p>
  
  <p>This is my application context (ONE-servletConfig.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" xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
      http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd"&gt;
  &lt;mvc:annotation-driven /&gt;
  
  &lt;context:annotation-config/&gt;
  &lt;context:component-scan base-package="nl.company.department.project.controller.controllers,nl.company.department.project.mongodb"&gt;&lt;/context:component-scan&gt;
  
  &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property value="WEB-INF/jsp/" name="prefix" /&gt;
      &lt;property value=".jsp" name="suffix" /&gt;
  &lt;/bean&gt;
  &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt;
  &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /&gt;
  
  
  &lt;mongo:mongo-client id="mongo" host="abcdefgh.mlab.com"
      port="123456" credentials="apple:pie@appledb" /&gt;
  
  &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt;
      &lt;constructor-arg name="mongo" ref="mongo" /&gt;
      &lt;constructor-arg name="databaseName" value="opadb" /&gt;
  &lt;/bean&gt;
  
  &lt;bean
      class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer" /&gt;
  
  &lt;mongo:repositories base-package="nl.company.department.project.mongodb"
      mongo-template-ref="mongoTemplate" /&gt;
  
  &lt;bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
      &lt;property name="maxUploadSize" value="100000" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>My web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee" version="3.0"&gt;
  &lt;display-name&gt;ONE&lt;/display-name&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;ONEapp&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/config/ONE-servletConfig.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;ONEapp&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p></p>
  
  <p>My repositories is in the package nl.company.department.project.mongodb
  One of the repositories:</p>
  
  <pre><code>@Repository
  public interface UserRepository extends MongoRepository&lt;User, String&gt; {
  
  }
  </code></pre>
  
  <p>In my controller I have autowired this repository and when I use method on userCollection it will work en return values of the database. This is the controller:</p>
  
  <pre><code>@RestController
  @RequestMapping("consultants")
  public class OPAController {
  
      @Autowired
      private UserRepository userCollection;
  
      more code ...
  }
  </code></pre>
  
  <p>The user object looks like this:</p>
  
  <pre><code>@Document(collection="user")
  public class User {
      @Id
      @Getter @Setter private String name;
      @Getter @Setter private String password;
  }
  </code></pre>
  
  <p>I wanted to use Spring Security, so i created a couple of classes...</p>
  
  <p>The security config class:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private CustomAuthenticationProvider authProvider;
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.authenticationProvider(authProvider);
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http
              .authorizeRequests()
              .antMatchers("/consultants/sendPassword","/consultants/authenticateUser")
              .permitAll()
              .anyRequest().authenticated()
              .and()
              .formLogin()
              .and()
              .csrf().disable();
      }
  }
  </code></pre>
  
  <p>And the SecurityWebApplicationInitializer class:</p>
  
  <pre><code>public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
  
      public SecurityWebApplicationInitializer() {
      super(SecurityConfig.class);
      }
  }
  </code></pre>
  
  <p>And the CustomAuthenticationProvider class</p>
  
  <pre><code>@Component
  @EnableMongoRepositories(basePackages = "nl.company.department.project.mongodb")
  public class CustomAuthenticationProvider implements AuthenticationProvider {
  
      @Autowired
      private UserRepository userCollection;
  
      @Override
      public Authentication authenticate(Authentication authentication) throws AuthenticationException {
          String name = authentication.getName();
          String password = authentication.getCredentials().toString();
  
          if (userCollection.findOne(name) != null &amp;&amp; userCollection.findOne(name).getPassword().equals(password)) {
              return new UsernamePasswordAuthenticationToken(name, password, new ArrayList&lt;&gt;());
          } else {
              return null;
          }
      }
  
      @Override
      public boolean supports(Class&lt;?&gt; authentication) {
          return authentication.equals(UsernamePasswordAuthenticationToken.class);
      }
  }
  </code></pre>
  
  <p>So autowiring the repository in the controller works, but when I want to use the repository in CustomAuthenticationProvider class it will throw a the following exception: </p>
  
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customAuthenticationProvider': Unsatisfied dependency expressed through field 'userCollection': Error creating bean with name 'userRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' is defined; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot resolve reference to bean 'mongoTemplate' while setting bean property 'mongoOperations'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'mongoTemplate' is defined
  </code></pre>
  
  <p>But the strange part is that i did defined the mongoTemplate and it worked in the controller. Can you guys see what I did wrong, maybe i missed something in the configuration...? </p>