title: When to use Spring's @Repository annotation?
body: <p>When reading about <a href="https://www.baeldung.com/spring-data-jpa-query" rel="noreferrer">creating custom queries in Spring</a>, I noticed that none of the classes (<code>UserRepositoryCustom</code>, <code>UserRepositoryCustomImpl</code>, <code>UserRepository</code>) use the <code>@Repository</code> annotation.</p>
  
  <p>I was surprised by this, because usually all the Spring application classes are annotated by something for them to be detected and a proxy generated for them. That's how the aspect-oriented framework works in Java.</p>
  
  <p>Then I checked my old <em>working</em> code, and realized that my repository interface also does not use the annotation. Yet, it is <code>@Autowired</code> as a dependency into the controllers and other classes that use it. Example:</p>
  
  <pre class="lang-java prettyprint-override"><code>// AddressRepository.java
  public interface AddressRepository extends CrudRepository&lt;Address, String&gt; {
  }
  
  // Repositories.java
  @Getter // lombok
  @Component
  public class Repositories { // autowire this to have access to all repo's
      private final AddressRepository addressRepository;
  
      @Autowired
      public Repositories(AddressRepository addressRepository) {
          this.addressRepository = addressRepository;
      }
  }
  </code></pre>
  
  <p>This begs the questions: <strong>When should the annotation be used?</strong> and <strong>How come a non-annotated class is automatically detected by Spring's component scan?</strong></p>