What Are Spring Annotations?
Spring annotations are metadata markers that provide hints to the Spring container, allowing it to manage beans and configure various parts of the application. They are essential in eliminating the XML-based configuration overhead that was frequent in earlier versions of Spring.
Why Use Spring Annotations?
Reduced Configuration Complexity: Annotations allow developers to configure Spring beans and components with minimum XML-based configuration. This results in better, more concise code.
Improved Readability: Annotations make code more readable by giving context within the source. This assists developers in understanding the purpose and behavior of distinct components.
Compile-Time Checking: Annotations perform compile-time checks, minimizing the likelihood of runtime failures caused by configuration issues.
Faster Development: With annotations, developers can focus on creating business logic rather than XML configurations, resulting in faster development.
@ComponentScan: At the class level, this annotation instructs Spring to scan the specified base package for components, such as beans, and register them in the Spring context.
@Component: Annotate a class with @Component to indicate that it should be discovered and registered as a Spring bean automatically.
@Autowired: This annotation is used for dependency injection automatically. It injects a matched Spring container bean into the annotated field or constructor.
@Qualifier: Directs the autowiring process to consider attributes other than the type when determining which dependency to inject.
@Configuration: Declares a class as a configuration class, allowing it to provide bean definitions and other configuration settings.
@Bean: Declare a method that returns a bean using @Bean within a configuration class. Spring will oversee the lifecycle of the bean.
@Controller: The @Controller annotation is applied at the class level. This uses the @Component specialization. A class is identified as a web request handler by this. Web page serving is frequently done using it. It usually gives back a string indicating the redirection route. The @RequestMapping annotation is the most common pairing with it.
@Service: The use of it in classes is also common. The Spring is informed that class holds the business logic.
@Repository: It's an annotation at the class level. A DAO (Data Access Object) called the repository has direct access to the database. Everything pertaining to the database is handled by the repository.
@ModelAttribute: When used with a method, this attribute preloads the model with the data returned by the method. Binds a model attribute to a parameter when applied to a parameter.
@SpringBootApplication: A Spring Boot application's brains. It bootstraps the application and configures it by combining @Configuration, @ComponentScan, and @EnableAutoConfiguration.
@RequestMapping: It is used to map the web requests. It has many optional elements like consumes, header, method, name, params, path, produces, and value. We use it with the class as well as the method.
@GetMapping: It maps the HTTP GET requests on the specific handler method. It is used to create a web service endpoint that fetches It is used instead of using: @RequestMapping(method = RequestMethod.GET)
@PostMapping: It maps the HTTP POST requests on the specific handler method. It is used to create a web service endpoint that creates It is used instead of using: @RequestMapping(method = RequestMethod.POST)
@PutMapping: It maps the HTTP PUT requests on the specific handler method. It is used to create a web service endpoint that creates or updates It is used instead of using: @RequestMapping(method = RequestMethod.PUT)
@DeleteMapping: It maps the HTTP DELETE requests on the specific handler method. It is used to create a web service endpoint that deletes a resource. It is used instead of using: @RequestMapping(method = RequestMethod.DELETE)
@PatchMapping: It maps the HTTP PATCH requests on the specific handler method. It is used instead of using: @RequestMapping(method = RequestMethod.PATCH)
@RequestBody: It is used to bind HTTP request with an object in a method parameter. Internally it uses HTTP MessageConverters to convert the body of the request. When we annotate a method parameter with @RequestBody, the Spring framework binds the incoming HTTP request body to that parameter.
@ResponseBody: It binds the method return value to the response body. It tells the Spring Boot Framework to serialize a return an object into JSON and XML format.
@PathVariable: It is used to extract the values from the URI. It is most suitable for the RESTful web service, where the URL contains a path variable. We can define multiple @PathVariable in a method.
@RequestParam: It is used to extract the query parameters form the URL. It is also known as a query parameter. It is most suitable for web applications. It can specify default values if the query parameter is not present in the URL.
@RestController: It can be considered as a combination of @Controller and @ResponseBody annotations. The @RestController annotation is itself annotated with the @ResponseBody annotation. It eliminates the need for annotating each method with @ResponseBody.
Comments