Spring Boot Cheat Sheet

Spring Annotations with Code

@Repository :

  • Class Level Annotation
  • It can reach the database and do all the operations.
  • It make the connection between the database and the business logic.
  • DAO is a repository.
  • It is a marker interface.
@Repository
public class TestRepo{
   public void add(){
      System.out.println("Added");
   }
}

@Service :

  • Class Level Annotation
  • It is a marker interface.
  • It is a business logic.
  • It is a service layer.
  • It used to create a service layer.
@Service
public class TestService{
   public void service1(){
      //business code (iş kodları)
   }
}

@Autowired :

  • Field Level Annotation
  • It is used to inject the dependency.
  • It is used to inject the object.
  • It is used to inject the object reference.
  • Dependency Injection is a design pattern.
public class Brand{
   private int id;
   private String name;

   @Autowired
   public Brand(int id, String name){
     this.id = id;
     this.name = name;
   }
}

@Controller :

  • Class Level Annotation
  • It is a marker interface.
  • It is a controller layer.
  • It is used to create a controller layer.
  • It use with @RequestMapping annotation.
@Controller
@RequestMapping("/api/brands")
public class BrandsController{
   @GetMapping("/getall")
   public Employee getAll(){
       return brandService.getAll();
   }
}

@RequestMapping :

  • Method Level Annotation
  • It is used to map the HTTP request with specific method.
@Controller
@RequestMapping("/api/brands")
public class BrandsController{
   @GetMapping("/getall")
   public Employee getAll(){
       return brandService.getAll();
   }
}

@GetMapping :

  • Method Level Annotation
  • It is used to map the HTTP GET request with specific method.
  • It is used to get the data.
  • It is used to read the data.
  @GetMapping("/getall")
  public Employee getAll(){
      return brandService.getAll();
  }

@PostMapping :

  • Method Level Annotation
  • It is used to map the HTTP POST request with specific method.
  • It is used to add the data.
  • It is used to create the data.
  @PostMapping("/add")
  public void add(@RequestBody Brand brand){
      brandService.add(brand);
  }

@PutMapping :

  • Method Level Annotation
  • It is used to map the HTTP PUT request with specific method.
  • It is used to update the data.
  @PutMapping("/update")
  public void update(@RequestBody Brand brand){
      brandService.update(brand);
  }

@DeleteMapping :

  • Method Level Annotation
  • It is used to map the HTTP DELETE request with specific method.
  • It is used to delete the data.
  @DeleteMapping("/delete")
  public void delete(@RequestBody Brand brand){
      brandService.delete(brand);
  }

@PathVariable :

  • Method Level Annotation
  • It is used to get the data from the URL.
  • It is the most suitable for RESTful web service that contains a path variable.
  @GetMapping("/getbyid/{id}")
  public Brand getById(@PathVariable int id){
      return brandService.getById(id);
  }

@RequestBody:

  • It is used to get the data from the request body.
  • It is used to get the data from the HTTP request.
  • It is used to get the data from the HTTP request body.
  @PostMapping("/add")
  public void add(@RequestBody Brand brand){
      brandService.add(brand);
  }

@RequestParam:

  • It is used to get the data from the URL.
  • It is used to get the data from the URL query parameters.
  • It is also known as query parameter.
@GetMapping("/getbyid")
public Brand getById(@RequestParam int id){
    return brandService.getById(id);
}

@RestController:

  • Class Level Annotation
  • It is a marker interface.
  • It is a controller layer.
  • It is used to create a controller layer.
  • It use with @RequestMapping annotation.
  • It is a combination of @Controller and @ResponseBody annotations.
  • @RestController annotation is explained with @ResponseBody annotation.
  • @ResponseBody eliminates the need to add a comment to every method.
@RestController
@RequestMapping("/api/brands")
public class BrandsController{
   @GetMapping("/getall")
   public Employee getAll(){
       return brandService.getAll();
   }
}

Spring Annotations (Category-Based)

1. Core Spring Annotations

  • @Component – Generic stereotype for a Spring-managed component.
  • @Service – Specialization of @Component for service layer classes.
  • @Repository – Specialization of @Component for DAO layer.
  • @Controller – Marks a class as a Spring MVC controller.
  • @RestController – Combination of @Controller and @ResponseBody.

2. Dependency Injection (DI) Annotations

  • @Autowired – Automatically injects dependencies.
  • @Qualifier("beanName") – Specifies the bean to be injected.
  • @Primary – Gives higher priority to a bean when multiple candidates exist.
  • @Value("${property.name}") – Injects property values from configuration.
  • @Scope("singleton") / @Scope("prototype") – Defines bean scope.

3. Configuration & Bean Management

  • @Configuration – Marks a class as a configuration class.
  • @Bean – Defines a bean within @Configuration.
  • @ComponentScan("package.name") – Scans specified package for components.
  • @PropertySource("classpath:app.properties") – Loads properties from a file.

4. Spring Boot Specific Annotations

  • @SpringBootApplication – Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.
  • @EnableAutoConfiguration – Enables Spring Boot’s auto-configuration.
  • @ConditionalOnProperty(name = "feature.enabled", havingValue = "true") – Conditional bean initialization.
  • @SpringBootTest – Configures an application context for testing.

5. MVC & REST Annotations

  • @RequestMapping("/path") – Maps HTTP requests to controller methods.
  • @GetMapping("/path") – Maps HTTP GET requests.
  • @PostMapping("/path") – Maps HTTP POST requests.
  • @PutMapping("/path") – Maps HTTP PUT requests.
  • @DeleteMapping("/path") – Maps HTTP DELETE requests.
  • @RequestBody – Maps HTTP request body to a Java object.
  • @ResponseBody – Serializes Java object to HTTP response body.
  • @PathVariable("id") – Extracts values from the URL path.
  • @RequestParam("param") – Extracts query parameters from the URL.

6. Transaction & Database Annotations

  • @Transactional – Manages database transactions.
  • @EnableTransactionManagement – Enables transaction management.
  • @PersistenceContext – Injects an EntityManager for JPA operations.
  • @Query("SELECT u FROM User u WHERE u.name = :name") – Custom JPQL queries in Spring Data JPA.

7. Security Annotations

  • @PreAuthorize("hasRole('ADMIN')") – Restricts access based on roles.
  • @PostAuthorize("returnObject.owner == authentication.name") – Authorizes after method execution.
  • @Secured("ROLE_USER") – Role-based method security.
  • @EnableWebSecurity – Enables Spring Security.

8. Caching Annotations

  • @EnableCaching – Enables caching in the application.
  • @Cacheable("cacheName") – Caches method results.
  • @CacheEvict("cacheName") – Evicts cache entries.
  • @CachePut("cacheName") – Updates the cache without evicting.

9. Scheduling & Async Processing

  • @EnableScheduling – Enables scheduled tasks.
  • @Scheduled(fixedRate = 5000) – Runs a scheduled task every 5 seconds.
  • @Async – Executes method asynchronously.

10. Event Handling Annotations

  • @EventListener – Listens for application events.
  • @TransactionalEventListener – Handles transactional events.
Share with