title: How to fix “Field … required a bean of type … that could not be found” exception Spring Boot
body: <p>I am working with spring boot tutorial about @Autowired and @Primary annotation
  <br> Below you can find my main class</p>
  <pre><code>package com.example.springtest;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import service.Animal;
  
  @SpringBootApplication
  public class SpringtestApplication {
  @Autowired
  private Animal animal;
  
  public static void main(String[] args) {
      SpringApplication.run(SpringtestApplication.class, args);
  
  }
  
  }
  </code></pre>
  <p>Animal interface (the interface that i should autowired)</p>
  <pre><code>package service;
  
  public interface Animal {
  
  
        String characteristics();
  }
  </code></pre>
  <p>Dog class (the primary implementation of Animal interface)</p>
  <pre><code>package service;
  
  
  import org.springframework.context.annotation.Primary;
  import org.springframework.stereotype.Service;
  
  @Primary
  @Service
  public class Dog implements Animal {
  
      @Override
      public String characteristics() {
          return &quot;Bark&quot;;
      }
  }
  </code></pre>
  <p>Cat class (the second implementation of Animal interface)</p>
  <pre><code>package service;
  
  
  import org.springframework.stereotype.Service;
  
  @Service
  public class Cat implements Animal {
  
      @Override
      public String characteristics() {
          return &quot;Meow&quot;;
      }
  }
  </code></pre>
  <p>pom.xml</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
           xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.3.1.RELEASE&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.example&lt;/groupId&gt;
      &lt;artifactId&gt;springtest&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;springtest&lt;/name&gt;
      &lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
                      &lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  <p>When I run the app there is a problem with injection of animal and I get following error:</p>
  <pre><code>***************************
  APPLICATION FAILED TO START
  ***************************
  
  Description:
  
  Field animal in com.example.springtest.SpringtestApplication required a bean of type 'service.Animal' that could not be found.
  
  The injection point has the following annotations:
      - @org.springframework.beans.factory.annotation.Autowired(required=true)
  
  
  Action:
  
  Consider defining a bean of type 'service.Animal' in your configuration.
  </code></pre>
  <p>What could be the reason that Spring cannot do the autowiring?</p>