SpringBoot Annotations - jiquest

add

#

SpringBoot Annotations


1. Spring Boot Core Annotations

  1. @SpringBootApplication - Entry point of a Spring Boot application (combines @Configuration, @EnableAutoConfiguration, @ComponentScan).

  2. @EnableAutoConfiguration - Automatically configures Spring Boot based on dependencies in the classpath. 

  3. @Configuration - Marks a class as a source of bean definitions.

  4. @ComponentScan - Scans the specified package for Spring beans.

  5. @Value - Injects a value into a field from properties or environment variables.

  6. @Bean - Declares a bean in a configuration class.

  7. @Component - Marks a class as a Spring-managed component.

  8. @Service - A specialization of @Component, used for service layer classes.

  9. @Repository - A specialization of @Component, used for DAO classes.

  10. @Controller - Marks a class as a Spring MVC controller.

  11. @RestController - Combines @Controller and @ResponseBody for RESTful services.

  12. @ConfigurationProperties - Binds external configuration properties to a POJO.

  13. @Import - Imports one or more configuration classes.

  14. @PropertySource - Specifies the location of an additional property file.

  15. @ImportResource - Imports XML configuration into the Spring context.

  16. @EnableScheduling - Enables scheduling support for tasks.

  17. @EnableAsync - Enables asynchronous method execution using @Async.

  18. @SpringBootTest - Used for integration testing with Spring Boot.

  19. @TestConfiguration - A configuration class used in tests.

  20. @EnableAspectJAutoProxy - Enables AspectJ-based AOP support. 

2. Spring Dependency Injection (DI) Annotations

  1. @Autowired - Marks a dependency to be injected automatically.

  2. @Qualifier - Specifies which bean to inject when multiple candidates exist.

  3. @Inject - Java standard for dependency injection (similar to @Autowired).

  4. @Resource - Injects a bean by name.

  5. @Primary - Marks a bean to be the primary bean when there are multiple candidates.

  6. @Named - A Java EE alternative to @Component for DI.

  7. @MockBean - Mocks a Spring bean in testing.

  8. @InjectMocks - Injects mocked dependencies into the object under test.

  9. @Mock - Creates a mock of a class for unit testing.

  10. @Spy - Creates a spy object to track method calls in testing.

3. Spring Security Annotations

  1. @EnableWebSecurity - Enables Spring Security for the application.

  2. @Secured - Specifies that a method or class can only be accessed by users with specific roles.

  3. @PreAuthorize - Defines security expressions for method-level access control.

  4. @PostAuthorize - Defines security checks after method execution.

  5. @RolesAllowed - Restricts access to methods based on roles.

  6. @EnableGlobalMethodSecurity - Enables method-level security annotations such as @Secured and @PreAuthorize.

  7. @EnableOAuth2Sso - Enables OAuth2 Single Sign-On support.

  8. @EnableGlobalAuthentication - Enables global authentication.

  9. @EnableOAuth2Login - Enables OAuth2 login functionality.

  10. @PreFilter - Filters parameters before method execution for security.

4. Spring Data JPA Annotations

  1. @Entity - Marks a class as a JPA entity.

  2. @Table - Specifies the table name for an entity.

  3. @Id - Specifies the primary key of the entity.

  4. @GeneratedValue - Specifies how the primary key is generated.

  5. @Column - Maps a field to a database column.

  6. @ManyToOne - Defines a many-to-one relationship.

  7. @OneToMany - Defines a one-to-many relationship.

  8. @OneToOne - Defines a one-to-one relationship.

  9. @ManyToMany - Defines a many-to-many relationship.

  10. @JoinColumn - Specifies the join column for relationships.

  11. @JoinTable - Defines a join table for a many-to-many relationship.

  12. @Transactional - Marks methods or classes as transactional.

  13. @Query - Defines custom queries in repository methods.

  14. @TransactionalEventListener - Listens for transactional events.

  15. @Embeddable - Marks a class as embeddable in entities.

  16. @EmbeddedId - Specifies a composite primary key.

  17. @OrderBy - Specifies the order of results in a query.

  18. @ElementCollection - Defines a collection of simple values or embeddable objects.

  19. @NotFound - Specifies how to handle a missing relationship in JPA.

  20. @Fetch - Defines how related entities are fetched (eager/lazy).

5. Spring Web Annotations

  1. @RequestMapping - Maps HTTP requests to handler methods.

  2. @GetMapping - Maps HTTP GET requests to handler methods.

  3. @PostMapping - Maps HTTP POST requests to handler methods.

  4. @PutMapping - Maps HTTP PUT requests to handler methods.

  5. @DeleteMapping - Maps HTTP DELETE requests to handler methods.

  6. @PatchMapping - Maps HTTP PATCH requests to handler methods.

  7. @RequestParam - Binds request query parameters to method parameters.

  8. @PathVariable - Binds path variables in the URL to method parameters.

  9. @RequestBody - Binds HTTP request body to method parameters.

  10. @ResponseBody - Marks method return values to be written to the HTTP response body.

  11. @RequestHeader - Binds HTTP request headers to method parameters.

  12. @CookieValue - Binds cookies to method parameters.

  13. @RequestMapping(path = "/path/{id}") - Maps path variables in the URL to method parameters.

  14. @ModelAttribute - Binds request parameters or model attributes to method parameters.

  15. @ResponseStatus - Marks a method or exception handler with a specific HTTP status code.

  16. @ExceptionHandler - Handles exceptions thrown in controller methods.

  17. @RestControllerAdvice - Provides centralized exception handling for REST controllers.

  18. @SessionAttributes - Binds model attributes to the HTTP session.

  19. @CrossOrigin - Enables Cross-Origin Resource Sharing (CORS) for controller methods.

  20. @EnableWebSecurity - Enables Spring Security for your web application.

6. Spring WebFlux Annotations

  1. @EnableWebFlux - Enables Spring WebFlux for reactive programming.

  2. @RestController - A combination of @Controller and @ResponseBody.

  3. @RequestMapping - Maps HTTP requests to handler methods.

  4. @GetMapping / @PostMapping - HTTP method-specific mappings.

  5. @ResponseBody - Marks method return values to be written to the HTTP response body.

  6. @RequestBody - Binds HTTP request body to a method parameter.

  7. @PathVariable - Binds path variables to method parameters.

  8. @RequestParam - Binds request query parameters to method parameters.

  9. @RequestHeader - Binds request headers to method parameters.

  10. @WebFluxTest - Focuses on testing WebFlux controllers in isolation.

7. Spring Actuator Annotations

  1. @Endpoint - Defines a custom actuator endpoint.

  2. @ReadOperation - Marks a method as a read operation in a custom endpoint.

  3. @WriteOperation - Marks a method as a write operation in a custom endpoint.

  4. @DeleteOperation - Marks a method as a delete operation in a custom endpoint.

  5. @HealthIndicator - Provides a custom health check for the actuator.

  6. @Sensitive - Marks data as sensitive and excludes it from actuator endpoints.

  7. @AuditEvent - Defines custom audit events.

  8. @MetricTag - Adds tags to metrics in Spring Actuator.

  9. @EnableActuator - Enables Spring Boot Actuator for monitoring and management.

  10. @NonNull - Used for marking a field or method argument that cannot be null.

8. Spring AOP (Aspect-Oriented Programming) Annotations

  1. @Aspect - Marks a class as an aspect for AOP.

  2. @Before - Defines advice to be executed before a method execution.

  3. @After - Defines advice to be executed after a method execution.

  4. @AfterReturning - Defines advice that runs after a method completes successfully.

  5. @AfterThrowing - Defines advice that runs if a method throws an exception.

  6. @Around - Defines advice that wraps around a method execution.

  7. @DeclareParents - Used to introduce new functionality to a class (aspect).

  8. @Order - Specifies the order of advice execution.

  9. @AroundAdvice - Defines an around advice for method execution.

  10. @DeclareError - Introduces error handling into classes.

9. Spring Testing Annotations

  1. @SpringBootTest - Loads the full application context for integration testing.

  2. @WebMvcTest - Focuses on testing Spring MVC controllers.

  3. @DataJpaTest - Focuses on testing JPA repositories.

  4. @MockBean - Mocks a Spring bean for testing purposes.

  5. @TestConfiguration - Declares a configuration class for testing.

  6. @BeforeEach / @AfterEach - Set up and tear down methods before/after each test.

  7. @BeforeAll / @AfterAll - Set up and tear down methods before/after all tests.

  8. @ContextConfiguration - Specifies context configuration for tests.

  9. @ActiveProfiles - Activates a specific Spring profile for testing.

  10. @Transactional - Marks a test to run within a transaction, automatically rolling back after test execution.

10. Spring Validation Annotations

  1. @NotNull - Ensures that a field is not null.

  2. @NotEmpty - Ensures that a string, collection, or array is not empty.

  3. @NotBlank - Ensures that a string is not blank (doesn't allow only whitespace).

  4. @Size - Ensures the size of a string, collection, or array is within a specified range.

  5. @Min / @Max - Ensures the value of a number is within the specified range.

  6. @Email - Ensures the value is a valid email format.

  7. @Pattern - Ensures that the string matches a given regular expression.

  8. @Future / @Past - Ensures that a date or time is in the future or past.

  9. @AssertTrue / @AssertFalse - Ensures that a boolean field is true or false.

  10. @DecimalMin / @DecimalMax - Ensures that a decimal value is within the specified range.