Back to Spring Boot Course

Spring Boot Course

Spring Boot Configuration Annotations

@SpringBootApplication

Combines @Configuration, @EnableAutoConfiguration, and @ComponentScan.

Marks the main application class

@EnableAutoConfiguration

Enables Spring Boot’s auto-configuration mechanism.

@PropertySource

Loads properties files into the Spring Environment.

@ConfigurationProperties

Binds properties from application.properties/application.yml to a Java object.

Core Spring Annotations

@Configuration

Marks a class as a source of bean definitions.

@ComponentScan

Configures component scanning for Spring Beans.

@Bean

Indicates that a method produces a bean to be managed by the Spring container.

@RestController

Combination of @Controller and @ResponseBody. Used for REST APIs.

@Controller

Marks a class as a Spring MVC Controller.

@RequestMapping

Maps HTTP requests to handler methods.

Variants: @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping.

@ResponseBody

Indicates that the return value of a method should be written directly to the HTTP response.

@RequestBody

Binds the HTTP request body to a method parameter.

@RequestParam

Binds a request parameter to a method parameter.

@PathVariable

Binds a URI template variable to a method parameter.

@ExceptionHandler

Handles exceptions for a specific controller.

Dependency Injection and Stereotypes

@Component

Indicates a Spring-managed component.

@Service

Specialization of @Component for service classes.

@Repository

Specialization of @Component for persistence classes.

@Autowired

Automatically injects dependencies.

@Qualifier

Specifies which bean to inject when multiple candidates exist.

@Primary

Indicates the default bean to use when multiple candidates exist.

Spring Data JPA Annotations

@Entity

Marks a class as a JPA entity.

@Table

Specifies the table associated with a JPA entity.

@Id

Marks a field as the primary key.

@GeneratedValue

Specifies the primary key generation strategy.

@Column

Maps a field to a column in the database.

@ManyToOne, @OneToMany, @OneToOne, @ManyToMany

Define relationships between entities.

@Repository

Indicates a repository that interacts with the database.

@Query

Defines a custom SQL or JPQL query.

@Transactional

Manages transaction boundaries.

Validation Annotations (Hibernate Validator)

@Valid

Triggers validation for an object.

@NotNull

Ensures a field is not null.

@NotEmpty

Ensures a field is not empty (for collections or strings).

@NotBlank

Ensures a string is not blank.

@Size

Validates the size of a collection, map, array, or string.

@Min, @Max

Validates minimum and maximum values.

@Pattern

Validates a string using a regex.

Spring Security Annotations

@EnableWebSecurity

Enables Spring Security in the application.

@Secured

Specifies roles allowed to execute a method.

@PreAuthorize

Specifies a SpEL expression to control access before a method is invoked.

@PostAuthorize

Specifies a SpEL expression to control access after a method is invoked.

Spring Test Annotations

@SpringBootTest

Loads the complete application context for testing.

@WebMvcTest

Loads only the web layer for testing.

@MockBean

Creates a mock and adds it to the Spring ApplicationContext.

@TestConfiguration

Custom configuration for test classes.

Scheduling and Asynchronous Annotations

@EnableScheduling

Enables scheduled tasks in the application.

@Scheduled

Schedules a method to run at fixed intervals or cron expressions.

@Async

Executes a method asynchronously.

Caching Annotations

@EnableCaching

Enables caching in the application.

@Cacheable

Indicates a method whose result should be cached.

@CacheEvict

Indicates a method to evict cache entries.

@CachePut

Updates the cache without skipping the method execution.

Miscellaneous

@Profile

Activates a bean only for specific profiles.

@Conditional

Registers a bean conditionally based on the context.

@Value

Injects values from properties files or environment variables.

@CrossOrigin

It enables cross-origin resource sharing only for this specific method.

@Scope

To change this default behavior, use @Scope spring annotation.

@SessionAttributes

Lists the names of model attributes which should be transparently stored in the session

@PreDestroy and @PostConstruct

Alternative way for bean initMethod and destroyMethod.

@Consumes

Used to specify the MIME media types of representations a resource can consume.

@Produces

Used to specify the MIME media types of representations a resource can produce

@Validated

To tell Spring to validate parameters that are passed into a method of the annotated class.