Spring Boot and Spring Framework Annotations Cheat Sheet

Here's a list of commonly used annotations in the Spring Framework and Spring Boot.

Spring Framework Annotations

@Component: Marks a class as a candidate for auto-detection as a Spring-managed bean. 

@Controller: Marks a class as a controller component in the MVC pattern. 

@Service: Marks a class as a service component in the business layer. 

@Repository: Marks a class as a repository component in the persistence layer. 

@Autowired: Injects dependencies automatically by type. 

@Qualifier: Specifies the specific bean to be autowired when multiple beans of the same type are available. 

@Value: Injects values from properties files or environment variables. 

@Configuration: Indicates that a class declares Spring configuration. @Bean: Marks a method as a provider of beans that should be managed by the Spring container. 

@ComponentScan: Configures component scanning for automatic bean detection. 

@RequestMapping: Maps HTTP requests to specific handler methods. 

@PathVariable: Binds a method parameter to a path variable in a request URL. 

@RequestParam: Binds a method parameter to a query parameter or form data in a request. 

@RequestBody: Binds the body of a request to a method parameter. 

@ResponseBody: Indicates that a method return value should be serialized directly to the HTTP response.

@GetMapping: Used to map HTTP GET requests to specific handler methods in a controller class.

@PostMapping: Used to map HTTP POST requests to specific handler methods in a controller class.

@PutMapping: Used to map HTTP PUT requests to specific handler methods in a controller class.

@DeleteMapping: Used to map HTTP DELETE requests to specific handler methods in a controller class.

@PatchMapping: Used to map HTTP PATCH requests to specific handler methods in a controller class.

@ExceptionHandler: Handles exceptions thrown by controller methods. 

@ResponseStatus: Sets the HTTP response status code for a controller method. 

@Aspect: Declares an aspect, combining advice and pointcuts. 

@Transactional: Specifies that a method (or all methods in a class) should be executed within a transactional context. 

@Async: Enables asynchronous execution of a method. 

Spring Boot Annotations

@SpringBootApplication: Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan, marking the main class as the entry point for a Spring Boot application.

 @EnableAutoConfiguration: Enables auto-configuration of the Spring application context, based on the classpath and defined dependencies. 

@ConfigurationProperties: Binds and validates externalized configuration properties to a configuration class. 

@ConditionalOnProperty: Configures beans based on the presence or absence of specified properties. 

@ConditionalOnClass: Configures beans based on the presence or absence of specified classes. 

@ConditionalOnBean: Configures beans based on the presence or absence of specified beans. 

@ConditionalOnMissingBean: Configures beans only if the specified beans are not present. 

@ConditionalOnExpression: Configures beans based on a SpEL expression. 

@Conditional: Specifies custom conditions for bean configuration. 

@EnableConfigurationProperties: Enables support for @ConfigurationProperties annotated classes. 

Spring Framework Annotations Cheat Sheet

These are just a few examples of the annotations provided by the Spring Framework and Spring Boot. There are many more annotations available for different purposes, such as security, messaging, caching, testing, and more. The specific annotations you use will depend on the modules and features of the Spring Framework or Spring Boot that you are utilizing in your application.

Comments