Spring Boot Annotations Cheat Sheet

In Java

Spring Boot is a popular framework for building Java applications. It provides a lot of features that make it easy to develop and deploy applications quickly. One of the key features of Spring Boot is its support for annotations. Annotations are a way to add metadata to your code that can be used by the Spring framework to configure your application.

There are many annotations available in Spring Boot, each with its own purpose. Here are some of the most commonly used annotations:

  1. @SpringBootApplication: This annotation is used to mark the main class of your Spring Boot application. It combines three other annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan.
  2. @RestController: This annotation is used to mark a class as a REST controller. It tells Spring that this class will handle HTTP requests and responses.
  3. @RequestMapping: This annotation is used to map a URL to a method in a controller. You can specify the HTTP method (GET, POST, etc.) and the URL path.
  4. @Autowired: This annotation is used to inject dependencies into a class. When you use this annotation, Spring will automatically create an instance of the dependency and inject it into the class.
  5. @Value: This annotation is used to inject values from a properties file or environment variable into a class. You can use this annotation to configure your application without hardcoding values in your code.

This cheat sheet provides an overview of the most commonly used Spring Boot annotations. The annotations are grouped by theme and presented in a table format.

Table of Contents

Configuration

AnnotationDescription
@SpringBootApplicationIndicates the main class of a Spring Boot application.
@ConfigurationIndicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
@EnableAutoConfigurationEnables Spring Boot’s auto-configuration mechanism.
@ComponentScanConfigures component scanning directives for use with @Configuration classes.
@PropertySourceAnnotation providing a convenient and declarative mechanism for adding a PropertySource to Spring’s Environment.
@ValueAnnotation at the field or method/constructor parameter level that indicates a default value expression for the affected argument.

Web

AnnotationDescription
@RestControllerIndicates that a class is a “”Controller”” (e.g. a web controller).
@RequestMappingAnnotation for mapping web requests onto methods in request-handling classes with flexible method signatures.
@GetMappingAnnotation for mapping HTTP GET requests onto specific handler methods.
@PostMappingAnnotation for mapping HTTP POST requests onto specific handler methods.
@PutMappingAnnotation for mapping HTTP PUT requests onto specific handler methods.
@DeleteMappingAnnotation for mapping HTTP DELETE requests onto specific handler methods.
@PathVariableAnnotation which indicates that a method parameter should be bound to a URI template variable.
@RequestParamAnnotation which indicates that a method parameter should be bound to a web request parameter.
@RequestBodyAnnotation indicating a method parameter should be bound to the body of the web request.
@ResponseBodyAnnotation indicating a method return value should be bound to the web response body.
@ResponseStatusAnnotation that marks a method or exception class with the status code and reason that should be returned.

Data

AnnotationDescription
@EntityIndicates that a class is an entity.
@RepositoryIndicates that an annotated class is a “”Repository””, originally defined by Domain-Driven Design (Evans, 2003) as “”a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects””.
@ServiceIndicates that an annotated class is a “”Service””, originally defined by Domain-Driven Design (Evans, 2003) as “”an operation offered as an interface that stands alone in the model, with no encapsulated state””.
@TransactionalAnnotation that can be applied to classes and methods to indicate that they should be run in a transactional context.
@AutowiredMarks a constructor, field, or setter method as to be autowired by Spring’s dependency injection facilities.
@QualifierAnnotation used to differentiate beans of the same type.

Security

AnnotationDescription
@EnableWebSecurityEnables Spring Security’s web security support and provides the Spring MVC integration.
@EnableGlobalMethodSecurityEnables Spring Security’s global method security.
@SecuredAnnotation that defines a method-level security configuration.
@PreAuthorizeAnnotation that indicates the method is to be pre-authorized before invocation.
@PostAuthorizeAnnotation that indicates the method is to be post-authorized after invocation.
@RolesAllowedAnnotation used to specify the roles allowed to access a method.

Testing

AnnotationDescription
@SpringBootTestAnnotation that can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features.
@WebMvcTestAnnotation that can be used for a Spring MVC test that focuses only on Spring MVC components.
@DataJpaTestAnnotation that can be used in combination with @RunWith(SpringRunner.class) for a typical JPA test.
@MockBeanAnnotation that can be used to add mock objects to the Spring application context.
@AutoConfigureMockMvcAnnotation that can be used to automatically configure a MockMvc instance.

References