Spring Boot Annotations

Frequently used Spring Boot Annotation with details and its usage category wise:

1] Core:

  • @SpringBootApplication : Bootstrap the app with default configurations, auto-config, component scan - Used on Main class 
  • @ComponentScan : Scan specific packages for Spring beans - Used on Config/Main class
  • @Configuration : Define Spring beans in Java config - Config class
  • @Bean : Declare & customize a bean manually - Method inside & config class
  • @Value : Inject values from application.properties file - Field, setter, constructor
  • @PropertySource : Load external properties file - Config class

2] DI (Dependency Injection) :

  • @Autowired : Auto inject dependencies by type - Field, setter, constructor
  • @Qualifier : Specify which bean to inject if multiple - Field, Param
  • @Primary : Mark default bean when multiple type is exist - Bean class or method
  • @Inject : Java standard alternatives to @Autowired - Field
  • @Resource : Inject beans by name - Field

3] Stereotype:

  • @Component : General purpose Spring manged bean - Class 
  • @Service : Business logic layer bean - Class
  • @Repository : DAO class with exception translation - Class or Interfaces
  • @Controller : MVC controller returning views - Class
  • @RestController : Rest controller returning JSON/XML - Class

4] Web (MVC) for Restful API development:

  • @RequestMapping : Map HTTP requests to methods - Class/Methods 
  • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping : Shorthand for HTTP requests methods - Method
  • @PathVariable : Bind URI template variable - Method param
  • @RequestParam : Bind query param form field - Method param
  • @RequestBody : Bind HTTP request body as JSON to object - Method param
  • @ResponseBody : Return JSON/XML response body - Method
  • @ModelAttribute : Bind form data to model - Method param
  • @CrossOrigin : Allow CORS for APIs - Class or Methods

5] Validations:

  • @Valid : Trigger validations on DTOs - Method param 
  • @NotNull, @Size : Validate fields(JSR-303) - DTO Field

6] JPA (Java Persistence API):

  • @Entity : Mark model class as DB entity - Model class 
  • @Table @Column : Customized database table/column mappings - Class or Field
  • @Id @GeneratedValue : Primary key and auto ID configurations - Field
  • @Repository : Mark DAO for exceptional handling - Interface or class
  • @EnableJpaRepositories : Enable repo scanning - Config class
  • @Transactional : Define transactional boundaries - Methods or class
  • @Modifying @Query : Custom JPA queries or updates - Repository methods

7] Scheduling / Aync:

  • @EnableScheduling : Bootstrap the app with default configurations, auto-config, component scan - Config class
  • @Scheduled : Run method on cron or fixed time - Method
  • @EnableAsync : Enable @Async processing - Config class
  • @Async : Run method in separate method - Method 

Comments

Popular posts from this blog

Java Interview Question - New FAQs

Full Stack Java Developer - FAQ

DSA Interview Coding Questions