You are currently viewing Annotations used in Spring MVC [2021]

Annotations used in Spring MVC [2021]

Hey guys today in this post i am going explain about Spring, Spring MVC, Spring REST and Spring Boot annotations. So let’s begin..

What is annotation?

Annotations are primarily used by code that is inspecting other code. They are often used for modifying (i.e. decorating or wrapping) existing classes at run-time to change their behavior.

Let’s explore some of the most important and most commonly used spring annotations.


Core spring annotations

  • @Required: This indicates that the bean must be populated at configuration time with the required property.
  • @Qualifier: It avoids the confusion that occurs when more than one bean of the same type is created.
  • @Value: indicates a default value expression for the field or parameter to initialize the property with
  • @Bean: It is used to indicate that the method produces a bean to be managed by spring
  • @Autowired: It marks a constructor, filed, or setter method to be autowired by spring dependency injection.
  • @Configuration: It indicates that a class is a configuration class that may contain bean definitions.
  • @ComponentScan: This points at the packages to scan for annotated components
  • @Lazy: It initialize a bean that will be initialized only when it is requested for
  • @Component: It is a generic stereotype for a spring managed component that indicates a class as a bean
  • @Service: It means an annotated class is a service class that can execute business logic perform calculations or call external API’s
  • @Controller: This marks that class as a web controller, capable of handling requests
  • @Repository: This indicates that an annotated class is a repository that is an abstraction of data access and storage

Spring MVC and REST

  • @RequestMapping: It maps HTTP requests into a specific handler classes or methods. with the method argument, it can perform different HTTP operations.
  • @GetMapping: It is used to map HTTP GET request into specific handler methods.
  • @PutMapping: It is used to map HTTP PUT request into specific handler methods.
  • @PostMapping: It is used to map HTTP POST request into specific handler methods.
  • @PathMapping: It is used to map HTTP PATCH request into specific handler methods.
  • @DeleteMapping: It is used to map HTTP DELETE request into specific handler methods.

  • @CookieValue: It is a method level annotation that is used as an argument of a request mapping method
  • @CrossOrigin: It is used both with classes and methods to enable cross origin requests
  • @ExceptionHandler: It is used to define the class of exception it will catch
  • @MatrixVariable: It is used to annotate request handler method arguments so that spring can inject the relevant bits of matrix URI
  • @PathVariable: It is used to annotate request handler method arguments
  • @RequestAttribute: It is used to bind the request attribute to a handler method parameter
  • @RequestBody: It indicates that a method parameter should be bound to the value of the HTTP request body
  • @RequestHeader: It is used to map a controller parameter to request the header value
  • @RequestParam: It retrieves the URL parameter and maps it to the method argument.
  • @RequestPart: It can be used instead of @RequestParam to get the content of a specific multipart and bind to the method argument
  • @ResponseBody: It indicates that the result type should be written straight in the response body in whatever format you specify like JSON or XML
  • @RestController: It marks the class as a controller, where every method returns a domain object instead of a view. It is the combination of @Controller and @ResponseBody
  • @ResponseStatus: It marks a method or exception class with a status code and a reason that must be returned
  • @ControllerAdvice: This is used to define @ExceptionHandler, @InitBinder and @ModelAttribute methods that apply to all @RequestMapping methods
  • @RestControllerAdvice: It combines @ControllerAdvice and @ResponseBody
  • @SessionAttribute: It is used to bind the method parameter to a session attribute, and provides an access to the existing or permanent session attributes
  • @ModelAttribute: refers to a property of the Model object (the M in MVC 😉 so let’s say we have a form with a form backing object that is called “Person” Then you can have Spring MVC supply this object to a Controller method by using the @ModelAttribute annotation

Spring boot annotations

  • @SpringBootApplication: It is a combination of @EnableAutoConfiguration, @ComponentScan and @Configuration that enables autoconfiguration and component scanning
  • @EnableAutoConfiguration: It auto configures the bean that is present in the class path and configures it to run the methods.

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply