Spring Boot Annotations: A Guide to Essential Annotations

Saurabh Kundu
6 min readMar 30, 2023

--

Spring boot annotation guide

A comprehensive list of all the basic and most commonly used Spring Boot Annotations.

When building applications with Spring Boot, knowing its basic and commonly used annotations is crucial. These annotations offer a concise and expressive way to configure components, define dependencies, and customize application behaviour. By adding metadata to classes and methods, annotations allow Spring Boot to automatically wire up and configure the application.

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.

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

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

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

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

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

@Transactional: This annotation is used to specify that all the operations performed within the annotated method or class will be executed within a single transaction. This helps maintain consistency and atomicity of database operations and can prevent data inconsistencies in case of failures. Read more here.

@Cacheable: This annotation is used to cache the result of a method invocation based on its input arguments. The cached results are returned for subsequent invocations of the method with the same arguments, which can help improve application performance by reducing the number of times the method is executed.

@Async: This annotation is used to execute a method in a separate thread from the calling thread. This can help improve application performance by allowing the calling thread to continue with its execution while the asynchronous method runs in the background.

@SpringBootTest: This annotation can load the entire application context and test the behaviour of the application as a whole, including all the components that are wired together.

@MockBean: This annotation can be used in unit tests to mock a bean in the Spring application context. This means that any method calls on the mocked bean will not actually execute the implementation of that bean, but instead return default values specified by the mocking framework.

@SpyBean: This annotation can be used in unit tests to create a partial mock of a bean in the Spring application context. When a bean is spied, a new instance of that bean is created that retains the original implementation of the methods, but also allows customizing the method behaviour for testing purposes.

@ActiveProfiles: This annotation is used to activate specific Spring profiles during testing like IT for integration tests, mysql for mysql specific configs etc..

@TestPropertySource: This annotation is used to specify the location of a properties file for testing. It can be used to load test-specific configuration properties that are different from those used in the main application.

@Sql: This annotation is used to define SQL scripts to be executed before or after a test method or class. It allows for pre-loading of data or schema definition into an in-memory database for testing purposes.

@DirtiesContext: This annotation is used to indicate that the Spring application context should be reset after a test has run. There are different modes, default is AFTER_CLASS, which means that the context will be reset after all tests in the test class have run.
Other class level modes include
BEFORE_CLASS , BEFORE_EACH_TEST_METHOD , AFTER_EACH_TEST_METHOD .
Method level modes include
BEFORE_METHOD , AFTER_METHOD.
Hierarchy mode includes
EXHAUSTIVE , CURRENT_LEVEL .

I hope this quick reference guide to Spring Boot annotations has been helpful to you. If you found this article useful, please feel free to check out my other articles here or connect with me on LinkedIn.

--

--