title: extending MongoRepository
body: <p>I'm new to spring and confused as to how autowiring works on an interface that extends MongoRepository </p>
  
  <p>Here is the code:</p>
  
  <p><em>App.java</em></p>
  
  <pre><code>package com.db.mongo;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  
  
  @SpringBootApplication
  public class App implements CommandLineRunner
  {
      @Autowired
      private CustomerRepository repository;
  
      public static void main( String[] args )
      {
          SpringApplication.run(App.class, args);
      }
  
      public void run(String... args) throws Exception {
          // TODO Auto-generated method stub
          repository.deleteAll();
          repository.save(new Customer("Alice", "Smith"));
          repository.save(new Customer("Bob", "Smith"));
  
          System.out.println("Customers found with findAll():");
          System.out.println("-------------------------------");
          for (Customer customer : repository.findAll()) {
              System.out.println(customer);
          }
          System.out.println();
          System.out.println("Customer found with findByFirstName('Alice'):");
          System.out.println("--------------------------------");
          System.out.println(repository.findByFirstName("Alice"));
  
          System.out.println("Customers found with findByLastName('Smith'):");
          System.out.println("--------------------------------");
          for (Customer customer : repository.findByLastName("Smith")) {
              System.out.println(customer);
          }
      }
  }
  </code></pre>
  
  <p><em>CustomerRepository</em></p>
  
  <pre><code>package com.db.mongo;
  
  import java.util.List;
  
  import org.springframework.data.mongodb.repository.MongoRepository;
  
  public interface CustomerRepository extends MongoRepository  {
  
       public Customer findByFirstName(String firstName);
          public List&lt;Customer&gt; findByLastName(String lastName);
  }
  </code></pre>
  
  <p><em>Customer.java</em></p>
  
  <pre><code>package com.db.mongo;
  
  import org.springframework.data.annotation.Id;
  
  public class Customer {
      @Id
      public String id;
  
      public String firstName;
      public String lastName;
  
      public Customer() {}
  
      public Customer(String firstName, String lastName) {
          this.firstName = firstName;
          this.lastName = lastName;
      }
  
      @Override
      public String toString() {
          return String.format(
                  "Customer[id=%s, firstName='%s', lastName='%s']",
                  id, firstName, lastName);
      }
  }
  </code></pre>