title: Why isn't necessary @Repository for this Spring Boot web app?
body: <p>I'm learning about Spring Boot and JPA, Spring Data Rest, H2 database and I have found a tutorial. I try to understand it and it is an easy example but I don't understand something. Why isn't it necessary to put <code>@Repository</code> or <code>@Component</code> in the AlienRepo class? </p>
  
  <p>The repo object is injected in the <strong>AlienController</strong> class, and I know from previous tutorials that you need to use <code>@Component</code> or <code>@Repository</code>. I thought that it is mandatory to use this annotation.</p>
  
  <p>The controller:</p>
  
  <pre><code>package com.dgs.springbootjpa.controller;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  
  import com.dgs.springbootjpa.dao.AlienRepo;
  import com.dgs.springbootjpa.model.Alien;
  
  @Controller
  public class AlienController {
  
      @Autowired
      AlienRepo repo;
  
      @RequestMapping("/")
      public String home() {
  
          return "home.jsp";
      }
  
      @RequestMapping("/addAlien")
      public String addAlien(Alien alien) {
  
          repo.save(alien);
          return "home.jsp";
      }
  
  }
  </code></pre>
  
  <p>The POJO:</p>
  
  <pre><code>package com.dgs.springbootjpa.model;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  
  @Entity
  public class Alien {
  
      @Id
      private int aid;
  
      private String aname;
  
      public int getAid() {
          return aid;
      }
      public void setAid(int aid) {
          this.aid = aid;
      }
      public String getAname() {
          return aname;
      }
      public void setAname(String aname) {
          this.aname = aname;
      }
  
      @Override
      public String toString() {
  
          return "Alien [aid=" + aid + ", aname=" + aname + "]";
      }
  }
  </code></pre>
  
  <p>The dao interface:</p>
  
  <pre><code>package com.dgs.springbootjpa.dao;
  
  import org.springframework.data.repository.CrudRepository;
  
  import com.dgs.springbootjpa.model.Alien;
  
  public interface AlienRepo extends CrudRepository&lt;Alien, Integer&gt; {
  
  }
  </code></pre>
  
  <p>You can see that in the <strong>AlienRepo</strong> interface there is not <code>@Repository</code> or <code>@Component</code> and the code is working successfully. </p>
  
  <p>Thank you!</p>
  
  <p>Update---------------------------------------------</p>
  
  <p>I want to fetch data from database and I add the below code. I want to ask you why @RequestParam isn't mandatory? If I delete the @RequestParam the app is working successfully.</p>
  
  <pre><code>@RequestMapping("/getAlien")
  public ModelAndView addAlien(@RequestParam int aid) {
  
      ModelAndView mv = new ModelAndView("showAlien");
      Alien alien = repo.findById(aid).orElse(new Alien()); 
      mv.addObject(alien); 
      return mv;
  }
  </code></pre>