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:
@SpringBootApplication: This annotation is used to mark the main class of your Spring Boot application. It combines three other annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan.
@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.
@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.
@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.
@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.
Indicates the main class of a Spring Boot application.
@Configuration
Indicates 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.
@EnableAutoConfiguration
Enables Spring Boot’s auto-configuration mechanism.
@ComponentScan
Configures component scanning directives for use with @Configuration classes.
@PropertySource
Annotation providing a convenient and declarative mechanism for adding a PropertySource to Spring’s Environment.
@Value
Annotation at the field or method/constructor parameter level that indicates a default value expression for the affected argument.
Web
Annotation
Description
@RestController
Indicates that a class is a “”Controller”” (e.g. a web controller).
@RequestMapping
Annotation for mapping web requests onto methods in request-handling classes with flexible method signatures.
@GetMapping
Annotation for mapping HTTP GET requests onto specific handler methods.
@PostMapping
Annotation for mapping HTTP POST requests onto specific handler methods.
@PutMapping
Annotation for mapping HTTP PUT requests onto specific handler methods.
@DeleteMapping
Annotation for mapping HTTP DELETE requests onto specific handler methods.
@PathVariable
Annotation which indicates that a method parameter should be bound to a URI template variable.
@RequestParam
Annotation which indicates that a method parameter should be bound to a web request parameter.
@RequestBody
Annotation indicating a method parameter should be bound to the body of the web request.
@ResponseBody
Annotation indicating a method return value should be bound to the web response body.
@ResponseStatus
Annotation that marks a method or exception class with the status code and reason that should be returned.
Data
Annotation
Description
@Entity
Indicates that a class is an entity.
@Repository
Indicates 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””.
@Service
Indicates 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””.
@Transactional
Annotation that can be applied to classes and methods to indicate that they should be run in a transactional context.
@Autowired
Marks a constructor, field, or setter method as to be autowired by Spring’s dependency injection facilities.
@Qualifier
Annotation used to differentiate beans of the same type.
Security
Annotation
Description
@EnableWebSecurity
Enables Spring Security’s web security support and provides the Spring MVC integration.
@EnableGlobalMethodSecurity
Enables Spring Security’s global method security.
@Secured
Annotation that defines a method-level security configuration.
@PreAuthorize
Annotation that indicates the method is to be pre-authorized before invocation.
@PostAuthorize
Annotation that indicates the method is to be post-authorized after invocation.
@RolesAllowed
Annotation used to specify the roles allowed to access a method.
Testing
Annotation
Description
@SpringBootTest
Annotation that can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features.
@WebMvcTest
Annotation that can be used for a Spring MVC test that focuses only on Spring MVC components.
@DataJpaTest
Annotation that can be used in combination with @RunWith(SpringRunner.class) for a typical JPA test.
@MockBean
Annotation that can be used to add mock objects to the Spring application context.
@AutoConfigureMockMvc
Annotation that can be used to automatically configure a MockMvc instance.