Spring Annotations Cheat Sheet

In Spring

Spring annotations are a powerful tool for developers who use the Spring Framework. Annotations are a way to add metadata to code, which can be used by the framework to configure and manage the application. Spring annotations provide a way to simplify the configuration of Spring applications, making it easier to write and maintain code.

This cheat sheet provides an extensive list of Spring annotations with their descriptions.

Core Annotations

AnnotationDescription
@AutowiredMarks a constructor, field, or method as to be autowired by Spring’s dependency injection facilities.
@ComponentIndicates that an annotated class is a “”component””. Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
@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.
@ControllerIndicates that an annotated class is a “”Controller”” (e.g. a web controller).
@RepositoryIndicates that an annotated class is a “”Repository”” (e.g. a data access object).
@ServiceIndicates that an annotated class is a “”Service”” (e.g. a business service).
@ScopeSpecifies the scope of a bean.

Web Annotations

AnnotationDescription
@ControllerAdviceMarks a class as a global exception handler for all @RequestMapping methods.
@CrossOriginConfigures cross-origin resource sharing (CORS) for a web application.
@RequestMappingMaps HTTP requests to handler methods of a controller.
@RequestParamBinds the value of a request parameter to a method parameter.
@ResponseBodyIndicates that a method return value should be bound to the web response body.
@RestControllerA convenience annotation that combines @Controller and @ResponseBody.
@PathVariableBinds a URI template variable to a method parameter.
@GetMappingShortcut for @RequestMapping(method = RequestMethod.GET).
@PostMappingShortcut for @RequestMapping(method = RequestMethod.POST).
@PutMappingShortcut for @RequestMapping(method = RequestMethod.PUT).
@DeleteMappingShortcut for @RequestMapping(method = RequestMethod.DELETE).

Data Annotations

AnnotationDescription
@TransactionalMarks a method as transactional.
@EntityIndicates that an annotated class is a JPA entity.
@TableSpecifies the primary table for the annotated entity.
@ColumnSpecifies the mapped column for a persistent property or field.
@IdSpecifies the primary key of an entity.
@GeneratedValueProvides for the specification of generation strategies for the values of primary keys.
@ManyToOneDefines a many-to-one relationship between two entities.
@OneToManyDefines a one-to-many relationship between two entities.
@JoinColumnSpecifies a column for joining an entity association or element collection.

Security Annotations

AnnotationDescription
@EnableWebSecurityEnables Spring Security’s web security support and provides the Spring Security configuration.
@SecuredDefines a method-level security expression.
@PreAuthorizeDefines a method-level security expression that is evaluated before a method is invoked.
@PostAuthorizeDefines a method-level security expression that is evaluated after a method is invoked.
@RolesAllowedDefines a list of roles that are allowed to access a method.
@AuthenticationPrincipalProvides access to the currently authenticated user.

Test Annotations

AnnotationDescription
@RunWithSpecifies the test runner to use.
@SpringBootTestLoads the complete Spring application context for integration testing.
@WebMvcTestLoads only the web layer of a Spring application context for testing controllers.
@DataJpaTestLoads only the JPA layer of a Spring application context for testing repositories.
@MockBeanCreates a mock object for a bean in the Spring application context.
@AutowiredInjects a bean from the Spring application context into a test class.

Conclusion

This cheat sheet provides an extensive list of Spring annotations with their descriptions. It is a useful reference for developers who are working with Spring applications.

Reference:

https://docs.spring.io/spring-framework/docs/current/reference/html/