Important Spring Boot Annotation. In this article, we will summarize all the annotations that we use while building any application using spring boot. We will start with Spring Framework annotation then move on to Spring Boot and Rest. So, let’s get started.
Spring Core Annotation
@Configuration: It makes a class a source of beans definition. It is applied to classes.
@ComponentScan: It tells the spring to scan the package for configuration. It is used with @Configuration.
@Bean: It is a method-level annotation. It tells the spring container to manage the bean returned by the method.
@Required: It is applied to bean property setter methods. It fails the configuration if dependency is not injected. If you want to know in detail check this article https://codedec.com/tutorials/annotation-based-configuration-in-spring-framework/
@Autowired: It autowires the appropriate bean into class members. It is applied to fields, methods, and constructors. If you want to know in detail check this article https://codedec.com/tutorials/autowiring-using-annotation-in-spring/
@Qualifier: It filters what beans should be used to @Autowire a field. It is applied to constructors, fields, and methods.
@Lazy: Spring Boot initializes the autowired dependencies on the project startup. If we want to initialize bean on request we can mark the component class with @Lazy annotation.
@Value: @Value annotation is used to add value to the fields. It is used with fields, constructor, and method parameters.
Spring Stereotype Annotation
@Component: It is used to make Java class as a bean. @Component annotation indicates that the class is a “component”. Thus, Spring will auto-detect the class and will create an object. If you want to know in detail check this article https://codedec.com/tutorials/component-value-annotation-in-spring/
@Controller: It is a specialized version of @Component. It is used to mark the class as a request handler.
@Service: It is used to mark the class as a Service class. Service class basically contains business logic.
@Repository: It is used to annotate the class which interacts with the database.
Spring MVC and REST Annotation
@Controller: It is used to mark the class as an HTTP request handler.
@RestController: It is used to simplify the creation of REST APIs.
@ResponseBody: The @ResponseBody annotation tells the spring to bind method return value to the HTTP response object.
@RequestBody: The @RequestBody annotation binds the HTTP request with an object in a method parameter.
@RequestMapping: The @RequestMapping annotation is used to map the request to the handler method. It can be used with classes or methods. It has attributes such as consumes, header, method, name, params, path.
@GetMapping: The @GetMapping annotation is used to map the GET request to the handler method.
@PostMapping: The @PostMapping annotation is used to map the POST request to the handler method.
@PutMapping: The @PutMapping annotation is used to map the PUT request to the handler method.
@DeleteMapping: The @DeleteMapping annotation is used to map the DELETE request to the handler method.
@PatchMapping: The @PatchMapping annotation is used to map the PATCH request to the handler method.
@Pathvariable: The @Pathvaraiable annotation is used to extract values from the URI.
@RequestParam: The @RequestParam annotation is used to get the form data and bind them into the method parameter.
Spring Boot Annotation
@Configuration and @ComponentScan: @Configuration annotation is used to mark the class as bean definition. @ComponentScan It tells the spring to scan the package for configuration. It is used with @Configuration.
@EnableAutoConfiguration: It will automatically configure the projects by adding beans.
@SpringBootApplication: It is the combination of @Configuration, @ComponentScan, and @EnableAutoConfiguration. By default, when we create a Spring boot project the Application class is marked with @SpringBootApplication.
Thus, these are the important Spring Boot Annotation every Java Developers should know about.