title: How to extend SimpleMongoRepository from spring-data mongodb?
body: <p>I am trying to create a BaseDAO interface which can be extended by all DAOs. The project uses spring-data with mongodb. The problem is that if I make all the individual DAOs extend MongoRepository and not write an Implementation class, then everything works fine. But if I try to add the MongoRepository to the BaseDAO interface with generics, the app doesn't work anymore because the parameters required to instantiate SimpleMongoRepository are null. This is the code I have so far :</p>
  
  <p><strong>BaseDAO.java</strong></p>
  
  <pre><code>import com.test.mongodb.BaseEntity;
  import org.springframework.data.mongodb.repository.MongoRepository;
  import org.springframework.data.repository.NoRepositoryBean;
  
  import java.io.Serializable;
  
  @NoRepositoryBean
  public interface BaseDAO&lt;T extends BaseEntity, ID extends Serializable&gt; extends         MongoRepository&lt;T, ID&gt; {
      public T getTestObject(ID id);
  }
  </code></pre>
  
  <p><strong>BaseDAOImpl.java</strong></p>
  
  <pre><code>import com.test.mongodb.BaseEntity;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.data.mongodb.core.MongoTemplate;
  import org.springframework.data.mongodb.repository.query.EntityInformationCreator;
  import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
  import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
  import org.springframework.data.repository.NoRepositoryBean;
  
  import java.io.Serializable;
  
  @NoRepositoryBean
  public class BaseDAOImpl&lt;T extends BaseEntity, ID extends Serializable&gt; extends     SimpleMongoRepository&lt;T,
          ID&gt; implements BaseDAO&lt;T, ID&gt; {
  
      @Autowired
      private static MongoTemplate mongoTemplate;
  
      @Autowired
      private static EntityInformationCreator entityInformationCreator;
  
      public BaseDAOImpl(Class&lt;T&gt; type){
          super((MongoEntityInformation&lt;T, ID&gt;)     entityInformationCreator.getEntityInformation(type), mongoTemplate);
      }
  
      @Override
      public T getTestObject(ID id){
          return findOne(id);
      }
  }
  </code></pre>
  
  <p><strong>UserDAO.java</strong></p>
  
  <pre><code>import com.test.mongodb.User;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public interface UserDAO extends BaseDAO&lt;User, String&gt; {}
  </code></pre>
  
  <p><strong>UserDAOImpl.java</strong></p>
  
  <pre><code>import com.test.mongodb.User;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public class UserDAOImpl extends BaseDAOImpl&lt;User, String&gt; implements UserDAO {
  
      public UserDAOImpl(){
          super(User.class);
      }
  }
  </code></pre>
  
  <p><strong>applicationContext.xml</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://www.springframework.org/schema/beans"
         xmlns:mongo="http://www.springframework.org/schema/data/mongo"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
      http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd"&gt;
  
      &lt;!-- MongoFactoryBean instance --&gt;
      &lt;mongo:mongo host="localhost" port="27017" /&gt;
  
      &lt;mongo:db-factory dbname="bank" mongo-ref="mongo" /&gt;
  
      &lt;!-- MongoTemplate instance --&gt;
      &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt;
          &lt;constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" /&gt;
  
      &lt;bean id="entityInformationCreator" class="org.springframework.data.mongodb.repository.support.DefaultEntityInformationCreator"&gt;
          &lt;constructor-arg name="mappingContext" ref="mappingContext" /&gt;
      &lt;/bean&gt;
      &lt;mongo:repositories base-package="com.test.mongodb.repo"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>App.java</strong></p>
  
  <pre><code>public class App {
  
      public static void main(String[] args) {
          ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  
          UserRepository userRepository = context.getBean("userRepository", UserRepository.class);
  
          User user = new User("Test User");
  
          userRepository.save(user);
  
          String id = user.getId();
          System.out.println(id);
  
          System.out.println(userRepository.getTestObject(user.getId()));
      }
  }
  </code></pre>
  
  <p>So when I run it, I get a NPE in BaseDAOImpl because both the mongoTemplate and the entityInformationCreator are null. How do I load them in? I also looked at the Spring MongoDB reference document but it mostly says to follow the documentation for other kind of repositories. The only thing I could find there and other places online was to create a factory bean. So I tried with that as well : </p>
  
  <p><strong>MongoRepoFactoryBean.java</strong></p>
  
  <pre><code>import org.springframework.data.mongodb.core.MongoTemplate;
  import org.springframework.data.mongodb.repository.MongoRepository;
  import org.springframework.data.mongodb.repository.support.MongoRepositoryFactory;
  import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean;
  import org.springframework.data.repository.NoRepositoryBean;
  import org.springframework.data.repository.core.support.RepositoryFactorySupport;
  
  import java.io.Serializable;
  
  @NoRepositoryBean
  public class MongoRepoFactoryBean&lt;T extends MongoRepository&lt;?,?&gt;, ID extends
      Serializable&gt; extends MongoRepositoryFactoryBean {
  
      protected RepositoryFactorySupport createRepositoryFactory(Class&lt;T&gt; clazz, MongoTemplate mongoTemplate) {
          return new MongoRepoFactory(clazz, mongoTemplate);
      }
  
      private static class MongoRepoFactory extends MongoRepositoryFactory {
          private Class clazz;
          private MongoTemplate mongoTemplate;
  
          public MongoRepoFactory(Class clazz, MongoTemplate mongoTemplate) {
              super(mongoTemplate);
              this.mongoTemplate = mongoTemplate;
              this.clazz = clazz;
          }
  
          public Object getTargetRepository() {
              return new BaseDAOImpl(clazz);
          }
  
          public Class&lt;?&gt; getRepositoryBaseClass() {
              return BaseDAOImpl.class;
          }
      }
  }
  </code></pre>
  
  <p>and changed the applicationContext.xml with :</p>
  
  <pre><code>&lt;mongo:repositories base-package="com.test.mongodb.repo"
                      factory-class="com.test.mongodb.repo.MongoRepoFactoryBean"/&gt;
  </code></pre>
  
  <p>But that doesn't work either. I tried with JavaConfig as well, but I don't know how to set the factory-class when doing the configuration using annotations. What am I doing wrong? SimpleMongoRepository doesn't come with a default constructor. Is the problem in injecting static fields? </p>