0% found this document useful (0 votes)
478 views

Spring Boot Annotation

The document discusses various Spring Boot annotations used for configuration, component scanning, auto-wiring beans, mapping web requests, and more. Some of the key annotations mentioned include @Component, @Controller, @Service, @Repository, @RequestMapping, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @RequestBody, @ResponseBody, @PathVariable, and @RequestParam. The annotations help configure Spring components, map web requests to controller methods, inject dependencies between beans, and more.

Uploaded by

Kiran Sardesai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
478 views

Spring Boot Annotation

The document discusses various Spring Boot annotations used for configuration, component scanning, auto-wiring beans, mapping web requests, and more. Some of the key annotations mentioned include @Component, @Controller, @Service, @Repository, @RequestMapping, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @RequestBody, @ResponseBody, @PathVariable, and @RequestParam. The annotations help configure Spring components, map web requests to controller methods, inject dependencies between beans, and more.

Uploaded by

Kiran Sardesai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Spring Boot Annotations

Spring Boot Annotations is a form of metadata that provides data about a program.
In other words, annotations are used to provide supplemental information about a
program. It is not a part of the application that we develop. It does not have a direct
effect on the operation of the code they annotate. It does not change the action of
the compiled program.

In this section, we are going to discuss some important Spring Boot


Annotation that we will use later in this tutorial.

Core Spring Framework Annotations


@Required: It applies to the bean setter method. It indicates that the annotated
bean must be populated at configuration time with the required property, else it
throws an exception BeanInitilizationException.

Pause
Unmute
Loaded: 34.84%
Fullscreen
Cancel

Example

1. public class Machine   
2. {  
3. private Integer cost;  
4. @Required  
5. public void setCost(Integer cost)   
6. {  
7. this.cost = cost;  
8. }  
9. public Integer getCost()   
10.{  
11.return cost;  
12.}     
13.}  

@Autowired: Spring provides annotation-based auto-wiring by providing


@Autowired annotation. It is used to autowire spring bean on setter methods,
instance variable, and constructor. When we use @Autowired annotation, the spring
container auto-wires the bean by matching data-type.

Example
1. @Component  
2. public class Customer  
3. {  
4. private Person person;  
5. @Autowired  
6. public Customer(Person person)   
7. {   
8. this.person=person;  
9. }  
10.}  

@Configuration: It is a class-level annotation. The class annotated with


@Configuration used by Spring Containers as a source of bean definitions.

Example

1. @Configuration  
2. public class Vehicle  
3. {  
4. @BeanVehicle engine()  
5. {  
6. return new Vehicle();  
7. }  
8. }  

@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.

Example

1. @ComponentScan(basePackages = "com.javatpoint")  
2. @Configuration  
3. public class ScanComponent  
4. {  
5. // ...  
6. }  

@Bean: It is a method-level annotation. It is an alternative of XML <bean> tag. It


tells the method to produce a bean to be managed by Spring Container.

Example
1. @Bean  
2. public BeanExample beanExample()   
3. {  
4. return new BeanExample ();  
5. }  

Spring Framework Stereotype Annotations


@Component: It is a class-level annotation. It is used to mark a Java class as a
bean. A Java class annotated with @Component is found during the classpath. The
Spring Framework pick it up and configure it in the application context as a Spring
Bean.

Example

1. @Component  
2. public class Student  
3. {  
4. .......  
5. }  

@Controller: The @Controller is a class-level annotation. It is a specialization


of @Component. It marks a class as a web request handler. It is often used to
serve web pages. By default, it returns a string that indicates which route to
redirect. It is mostly used with @RequestMapping annotation.

Example

1. @Controller  
2. @RequestMapping("books")  
3. public class BooksController   
4. {  
5. @RequestMapping(value = "/{name}", method = RequestMethod.GET)  
6. public Employee getBooksByName()   
7. {  
8. return booksTemplate;  
9. }  
10.}  

@Service: It is also used at class level. It tells the Spring that class contains
the business logic.

Example
1. package com.javatpoint;  
2. @Service  
3. public class TestService  
4. {  
5. public void service1()  
6. {  
7. //business code  
8. }  
9. }  

@Repository: It is a class-level annotation. The repository is a DAOs (Data Access


Object) that access the database directly. The repository does all the operations
related to the database.

1. package com.javatpoint;  
2. @Repository   
3. public class TestRepository  
4. {  
5. public void delete()  
6. {     
7. //persistence code  
8. }  
9. }  

Spring Boot Annotations


o @EnableAutoConfiguration: It auto-configures the bean that is present in
the classpath and configures it to run the methods. The use of this annotation
is reduced in Spring Boot 1.2.0 release because developers provided an
alternative of the annotation, i.e. @SpringBootApplication.
o @SpringBootApplication: It is a combination of three
annotations @EnableAutoConfiguration,
@ComponentScan, and @Configuration.

Spring MVC and REST Annotations

o @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.
Example

1. @Controller  
2. public class BooksController   
3. {  
4. @RequestMapping("/computer-science/books")  
5. public String getAllBooks(Model model)  
6. {  
7. //application code  
8. return "bookList";  
9. }  

o @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)
o @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)
o @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)
o @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)
o @PatchMapping: It maps the HTTP PATCH requests on the specific handler
method. It is used instead of using: @RequestMapping(method =
RequestMethod.PATCH)
o @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.
o @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.
o @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.
o @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.
o @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
o @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.
o @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.

You might also like