Spring Boot Annotations
Important Annotations
Annotations are form of meta-data that provides data about a spring application.
By using annotations we can avoid xml configuration file.
Annotations are easy way to write make more readable.
Annotations are divided into my four types:
1.Core Spring Framework Annotations
@Bean: It is a method level annotation used to return a new bean(object).
@Configuration: It is a class-level annotation , it tells that class contains a bean definitions.
@ComponentScan: It is used when we want to scan a package for beans. It is used with the annotation @Configuration. We can also specify the base packages to scan for Spring Components.
@Required:It is a method level annotation, applied on setter methods to tell this field value is mandatory to contain value.
@Autowired: One of the mostly used annotation. It is applied on setter methods and instance variable , constructor. By applying this annotation, spring will automatically inject the bean into that class by matching data-type.
@Scope: It is a class level annotation used to define the scope of that class , whether it is a singleton, prototype.
@Value: This annotation helps to provide the value to variables.
2.Spring Framework Stereotype Annotations
What is mean by Stereotype?
an idea that is used to describe a particular type.
@Component: It is a class level annotation, by marking with this it is found during the class path, and spring framework will detect and configure in application context as bean.
@Service: It is a class level annotation, it tell that this class contains business logic.
Get Spring Techie’s stories in your inbox
Join Medium for free to get updates from this writer.
@Repository: It is a class level annotation, it tell that this class contains code to interact with database.
@Controller: It is a class level annotation, it tell that this class will act as a front-controller. All the incoming requests are handled by this class.
3.Spring MVC and REST Annotations:
- @GetMapping: It maps the HTTP GET requests on the handler method. It is used to create endpoint to fetch data..
- @PostMapping: It maps the HTTP POST requests on the handler method. It is used to create a web service endpoint to submit the data to server.
- @PutMapping: It maps the HTTP PUT requests on the handler method. It is used to create a web service endpoint to update data.
- @DeleteMapping: It maps the HTTP DELETE requests on the handler method. It is used to create a web service endpoint that deletes a resource.
- @PatchMapping: It maps the HTTP PATCH requests on the handler method. It will used to do partial update.
- @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.
- @RequestHeader: It is used to get the details about the HTTP request headers. We use this annotation as a method parameter. The optional elements of the annotation are name, required, value, defaultValue. For each detail in the header, we should specify separate annotations. We can use it multiple time in a method
- @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.
- @RequestAttribute: It binds a method parameter to request attribute. It provides convenient access to the request attributes from a controller method. With the help of @RequestAttribute annotation, we can access objects that are populated on the server-side.
4.Spring Boot Annotations
- @EnableAutoConfiguration: It auto-configures the bean that is present in the classpath and configures it to run the methods.
- @SpringBootApplication: It is a combination of three annotations @EnableAutoConfiguration, @ComponentScan, and @Configuration.
- With the @SpringBootApplication single annotation we perform @EnableAutoConfiguration, @ComponentScan, and @Configuration
……………………………………………Thank You……………………………………









