title: org.springframework.beans.factory.UnsatisfiedDependencyException: Unsatisfied dependency expressed through constructor argument with index 0
body: <p>I am creating a REST service to communicate and perform basic CRUD operations on mongodb. But I am receiver the following error:</p>
  
  <pre><code>Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoDBTodoService' defined in file [/Users/avarshney/Documents/Aayush/STS/rest_mongo/target/classes/rest_mongo/MongoDBTodoService.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [rest_mongo.TodoRepository]: : No qualifying bean of type [rest_mongo.TodoRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [rest_mongo.TodoRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
  at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
  at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1131)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1034)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
  at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
  at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
  at rest_mongo.TodoAppConfig.main(TodoAppConfig.java:14)
  
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [rest_mongo.TodoRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
  at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
  at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
  </code></pre>
  
  <p>Here are my respective classes:</p>
  
  <p><strong>TodoAppConfig.java</strong></p>
  
  <pre><code>package rest_mongo;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  
  @Configuration
  @EnableAutoConfiguration
  @ComponentScan
  public class TodoAppConfig {
  
      public static void main(String[] args) {
          SpringApplication.run(TodoAppConfig.class, args);
      }
  }
  </code></pre>
  
  <p><strong>TodoService.java</strong>
  (interface)</p>
  
  <pre><code>package rest_mongo;
  
  import java.util.List;
  
  interface TodoService {
  
      TodoDTO create(TodoDTO todo);
  
      TodoDTO delete(String id);
  
      List&lt;TodoDTO&gt; findAll();
  
      TodoDTO findById(String id);
  
      TodoDTO update(TodoDTO todo);
  }
  </code></pre>
  
  <p><strong>TodoRepository.java</strong>
  (interface)</p>
  
  <pre><code>package rest_mongo;
  
  import org.springframework.data.repository.Repository;
  
  import java.util.List;
  
  interface TodoRepository extends Repository&lt;Todo, String&gt; {
  
      void delete(Todo deleted);
  
      List&lt;Todo&gt; findAll();
  
      Todo findOne(String id);
  
      Todo save(Todo saved);
  }
  </code></pre>
  
  <p><strong>TodoDTO.java</strong></p>
  
  <pre><code>package rest_mongo;
  
  import org.hibernate.validator.constraints.NotEmpty;
  
  import javax.validation.constraints.Size;
  
  public final class TodoDTO {
  
      private String id;
  
      @Size(max = Todo.MAX_LENGTH_DESCRIPTION)
      private String description;
  
      @NotEmpty
      @Size(max = Todo.MAX_LENGTH_TITLE)
      private String title;
  
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
  
  }
  </code></pre>
  
  <p><strong>TodoController.java</strong></p>
  
  <pre><code>package rest_mongo;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestBody;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.ResponseStatus;
  import org.springframework.web.bind.annotation.RestController;
  
  import javax.validation.Valid;
  import java.util.List;
  
  @RestController
  @RequestMapping("/Users/avarshney/Documents/Aayush/STS/rest_mongo/target/classes/rest_mongo/todo")
  final class TodoController {
  
      private final TodoService service;
  
      @Autowired
      TodoController(TodoService service) {
          this.service = service;
      }
  
      @RequestMapping(method = RequestMethod.POST)
      @ResponseStatus(HttpStatus.CREATED)
      TodoDTO create(@RequestBody @Valid TodoDTO todoEntry) {
          return service.create(todoEntry);
      }
  
      @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
      TodoDTO delete(@PathVariable("id") String id) {
          return service.delete(id);
      }
  
      @RequestMapping(method = RequestMethod.GET)
      List&lt;TodoDTO&gt; findAll() {
          return service.findAll();
      }
  
      @RequestMapping(value = "{id}", method = RequestMethod.GET)
      TodoDTO findById(@PathVariable("id") String id) {
          return service.findById(id);
      }
  
      @RequestMapping(value = "{id}", method = RequestMethod.PUT)
      TodoDTO update(@RequestBody @Valid TodoDTO todoEntry) {
          return service.update(todoEntry);
      }
  
  }
  </code></pre>
  
  <p><strong>Todo.java</strong></p>
  
  <pre><code>package rest_mongo;
  
  import org.springframework.data.annotation.Id;
  
  final class Todo
  {
      static final int MAX_LENGTH_DESCRIPTION = 500;
      static final int MAX_LENGTH_TITLE = 100;
  
      @Id
      private String id;
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      private String description;
  
      private String title;
  
      public Todo() {}
  
      private Todo(Builder builder) {
          this.description = builder.description;
          this.title = builder.title;
      }
  
      static Builder getBuilder() {
          return new Builder();
      }
  
      //Other getters are omitted
  
      public void update(String title, String description) {
          this.title = title;
          this.description = description;
      }
  
      static class Builder {
  
          private String description;
  
          private String title;
  
          private Builder() {}
  
          Builder description(String description) {
              this.description = description;
              return this;
          }
  
          Builder title(String title) {
              this.title = title;
              return this;
          }
  
          Todo build() {
              Todo build = new Todo(this);
  
              return build;
          }
      }
  }
  </code></pre>
  
  <p><strong>MongoDBTodoService.java</strong></p>
  
  <pre><code>package rest_mongo;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import java.util.List;
  
  import static java.util.stream.Collectors.toList;
  
  @Service
  final class MongoDBTodoService implements TodoService {
  
      @Autowired
      private final TodoRepository repository;
  
      @Autowired
      MongoDBTodoService(TodoRepository repository) {
          this.repository = repository;
      }
  
      @Override
      public TodoDTO create(TodoDTO todo) {
          Todo persisted = Todo.getBuilder()
                  .title(todo.getTitle())
                  .description(todo.getDescription())
                  .build();
          persisted = repository.save(persisted);
          return convertToDTO(persisted);
      }
  
      @Override
      public TodoDTO delete(String id) {
          Todo deleted = findTodoById(id);
          repository.delete(deleted);
          return convertToDTO(deleted);
      }
  
      @Override
      public List&lt;TodoDTO&gt; findAll() {
          List&lt;Todo&gt; todoEntries = repository.findAll();
          return convertToDTOs(todoEntries);
      }
  
      private List&lt;TodoDTO&gt; convertToDTOs(List&lt;Todo&gt; models) {
          return models.stream()
                  .map(this::convertToDTO)
                  .collect(toList());
      }
  
      @Override
      public TodoDTO findById(String id) {
          Todo found = findTodoById(id);
          return convertToDTO(found);
      }
  
      @Override
      public TodoDTO update(TodoDTO todo) {
          Todo updated = findTodoById(todo.getId());
          updated.update(todo.getTitle(), todo.getDescription());
          updated = repository.save(updated);
          return convertToDTO(updated);
      } 
  
      private Todo findTodoById(String id) {
          Todo result = repository.findOne(id);
          return result;
  
      }
  
      private TodoDTO convertToDTO(Todo model) {
          TodoDTO dto = new TodoDTO();
  
          dto.setId(model.getId());
          dto.setTitle(model.getTitle());
          dto.setDescription(model.getDescription());
  
          return dto;
      }
  }
  </code></pre>
  
  <p>I am very new to this technology hence please tell me if there are some very obvious mistakes.
  I am referring to this link for this code : <a href="http://www.petrikainulainen.net/programming/spring-framework/creating-a-rest-api-with-spring-boot-and-mongodb/" rel="nofollow">http://www.petrikainulainen.net/programming/spring-framework/creating-a-rest-api-with-spring-boot-and-mongodb/</a></p>
  
  <p>Please tell me how to resolve this error. </p>