Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

14/04/2024, 19:27 Spring Boot Annotations: A Guide to Essential Annotations | by Saurabh Kundu | Medium

In this article, we’ll explore the most commonly used Spring Boot
annotations. Let’s begin.

@SpringBootApplication: This annotation is used to indicate that a class is a


Spring Boot application. It includes @Configuration , @ComponentScan and
@EnableAutoConfiguration .

@Configuration: This annotation is used to indicate that a class is a


configuration class. It is typically used in combination with @Bean methods to
define Spring beans.

@ComponentScan: This annotation is used to specify the packages that should be


scanned for Spring components.

@EnabeAutoConfiguration: This annotation is used to enable Spring Boot’s


auto-configuration feature. It automatically configures Spring beans based on the
dependencies in your classpath.

@Bean: This annotation is used to declare a Spring Bean explicitly.

@Autowired: This annotation is used to inject a bean into another bean.

https://medium.com/@saurabh.kundu/spring-boot-annotations-a-guide-to-essential-annotations-b4d18a26763a 3/18
14/04/2024, 19:27 Spring Boot Annotations: A Guide to Essential Annotations | by Saurabh Kundu | Medium

@Qualifier: This annotation is used to specify which bean should be injected when
multiple beans of the same type are available in the Spring application context.

@Primary: This annotation is used to indicate a primary bean when there are
multiple beans of the same type.

@Lazy: This annotation is used to indicate that a bean should be initialized only
when it is first requested by the application. This can help improve performance by
reducing the startup time of the application.

@Validated: This annotation allows Spring to validate any method arguments


that are annotated with the Bean Validation @NotNull , @Min , @Max , @Size , and
other constraint annotations

@Component: This annotation is used to mark a class as a Spring component.

@Controller: This annotation is used to indicate a class that handles HTTP


requests in a web application.

https://medium.com/@saurabh.kundu/spring-boot-annotations-a-guide-to-essential-annotations-b4d18a26763a 4/18
14/04/2024, 19:27 Spring Boot Annotations: A Guide to Essential Annotations | by Saurabh Kundu | Medium

@Service: This annotation is used to indicate a class that provide business logic or
perform complex tasks in an application.

@Repository: This annotation is used to indicate that a class is used to perform


database operations.

@ConditionalOnClass: This annotation checks if a specific class is present in the


classpath. It allows a bean to be created only if a particular class is available.

@ConditionalOnBean: This annotation checks if a specific bean is present in the


application context. It allows a bean to be created only if another bean is already
available in the context.

@ConditionalOnExpression: This annotation checks if a specific Spring


Expression Language (SpEL) expression evaluates to true. It allows a bean to be
created only if a certain condition is met.

@ConditionalOnMissingBean: This annotation checks if a specific bean is


missing in the application context. It allows a bean to be created only if a

https://medium.com/@saurabh.kundu/spring-boot-annotations-a-guide-to-essential-annotations-b4d18a26763a 5/18
14/04/2024, 19:27 Spring Boot Annotations: A Guide to Essential Annotations | by Saurabh Kundu | Medium

particular bean is not already available in the context.

@ConditionalOnProperty: This annotation checks if a specific configuration


property is set. It allows a bean to be created only if a certain configuration
property is present.

@ConditionalOnWebApplication: This annotation checks if the application is a


web application or not. It allows a bean to be created only if the application is a
web application.

@RestController: This annotation is used to define a class as a RESTful web


service controller. It is a combination of @Controller and @ResponseBody

annotations.

@RequestMapping: This annotation is used to map a URL request to a method in


a controller. It can be used to specify HTTP methods, headers, parameters, and
other properties of the request.

https://medium.com/@saurabh.kundu/spring-boot-annotations-a-guide-to-essential-annotations-b4d18a26763a 6/18
14/04/2024, 19:27 Spring Boot Annotations: A Guide to Essential Annotations | by Saurabh Kundu | Medium

@PathVariable: This annotation is used to map a URL request parameter to a


method parameter in a controller.

@RequestParam: This annotation is used to map a URL query parameter to a


method parameter in a controller.

@RequestBody: This annotation is used to map the HTTP request body to a


method parameter in a controller.

@ResponseStatus: This annotation is used to set the HTTP response status code
for a controller method.

@GetMapping: This annotation is used to map HTTP GET requests to a specific


method in a controller class. It is often used to retrieve resources from a server.

@PostMapping: This annotation is used to map HTTP POST requests to a specific


method in a controller class. It is often used to create new resources on a server.

@PutMapping: This annotation is used to map HTTP PUT requests to a specific


method in a controller class. It is often used to update existing resources on a
server.

https://medium.com/@saurabh.kundu/spring-boot-annotations-a-guide-to-essential-annotations-b4d18a26763a 7/18
14/04/2024, 19:27 Spring Boot Annotations: A Guide to Essential Annotations | by Saurabh Kundu | Medium

@DeleteMapping: This annotation is used to map HTTP DELETE requests to a


specific method in a controller class. It is often used to delete resources from a
server.

@PatchMapping: This annotation is used to map HTTP PATCH requests to a


specific method in a controller class. It is often used to partially update existing
resources on a server.

@RestControllerAdvice: This annotation is used to define a class that handles


exceptions globally for all RESTful controllers in the application. It allows for
centralized handling of exceptions and can provide a consistent error response
format across the application.

@ExceptionHandler: This annotation is used to define a method that handles a


specific exception thrown by a controller. It allows for fine-grained exception
handling and can provide customized error responses based on the type of
exception thrown.

https://medium.com/@saurabh.kundu/spring-boot-annotations-a-guide-to-essential-annotations-b4d18a26763a 8/18
14/04/2024, 19:27 Spring Boot Annotations: A Guide to Essential Annotations | by Saurabh Kundu | Medium

@PropertySource: This annotation is used to specify the location of a properties


file to be loaded into the Spring Environment.

@Value: This annotation is used to inject a value from a property source into a
Spring bean. It can be used to retrieve a single value or to inject a collection of
values.

@ConfigurationProperties: This annotation is used to bind external


configuration properties to a Spring @Configuration class. It can be used to define
properties for an entire application or for a specific module within the application.

@ConfigurationPropertiesScan: This annotation is used to automatically scan


for classes annotated with @ConfigurationProperties and register them with the
Spring context. It simplifies the process of creating and configuring many
properties files.

@Import: This annotation brings configuration classes that are defined in other
packages, projects or libraries, into their own Spring configuration class.

https://medium.com/@saurabh.kundu/spring-boot-annotations-a-guide-to-essential-annotations-b4d18a26763a 9/18

You might also like