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
Annotation | Description |
---|---|
@Autowired | Marks a constructor, field, or method as to be autowired by Spring’s dependency injection facilities. |
@Component | Indicates that an annotated class is a “”component””. Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning. |
@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. |
@Controller | Indicates that an annotated class is a “”Controller”” (e.g. a web controller). |
@Repository | Indicates that an annotated class is a “”Repository”” (e.g. a data access object). |
@Service | Indicates that an annotated class is a “”Service”” (e.g. a business service). |
@Scope | Specifies the scope of a bean. |
Web Annotations
Annotation | Description |
---|---|
@ControllerAdvice | Marks a class as a global exception handler for all @RequestMapping methods. |
@CrossOrigin | Configures cross-origin resource sharing (CORS) for a web application. |
@RequestMapping | Maps HTTP requests to handler methods of a controller. |
@RequestParam | Binds the value of a request parameter to a method parameter. |
@ResponseBody | Indicates that a method return value should be bound to the web response body. |
@RestController | A convenience annotation that combines @Controller and @ResponseBody . |
@PathVariable | Binds a URI template variable to a method parameter. |
@GetMapping | Shortcut for @RequestMapping(method = RequestMethod.GET) . |
@PostMapping | Shortcut for @RequestMapping(method = RequestMethod.POST) . |
@PutMapping | Shortcut for @RequestMapping(method = RequestMethod.PUT) . |
@DeleteMapping | Shortcut for @RequestMapping(method = RequestMethod.DELETE) . |
Data Annotations
Annotation | Description |
---|---|
@Transactional | Marks a method as transactional. |
@Entity | Indicates that an annotated class is a JPA entity. |
@Table | Specifies the primary table for the annotated entity. |
@Column | Specifies the mapped column for a persistent property or field. |
@Id | Specifies the primary key of an entity. |
@GeneratedValue | Provides for the specification of generation strategies for the values of primary keys. |
@ManyToOne | Defines a many-to-one relationship between two entities. |
@OneToMany | Defines a one-to-many relationship between two entities. |
@JoinColumn | Specifies a column for joining an entity association or element collection. |
Security Annotations
Annotation | Description |
---|---|
@EnableWebSecurity | Enables Spring Security’s web security support and provides the Spring Security configuration. |
@Secured | Defines a method-level security expression. |
@PreAuthorize | Defines a method-level security expression that is evaluated before a method is invoked. |
@PostAuthorize | Defines a method-level security expression that is evaluated after a method is invoked. |
@RolesAllowed | Defines a list of roles that are allowed to access a method. |
@AuthenticationPrincipal | Provides access to the currently authenticated user. |
Test Annotations
Annotation | Description |
---|---|
@RunWith | Specifies the test runner to use. |
@SpringBootTest | Loads the complete Spring application context for integration testing. |
@WebMvcTest | Loads only the web layer of a Spring application context for testing controllers. |
@DataJpaTest | Loads only the JPA layer of a Spring application context for testing repositories. |
@MockBean | Creates a mock object for a bean in the Spring application context. |
@Autowired | Injects 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/