🚀 Spring Boot Annotations You MUST Know – With Real-World Use Cases
Spring Boot annotations are not just shortcuts, they define how your application starts, scales, secures, and behaves in production.
🔹 Application Bootstrapping:
@SpringBootApplication → Entry point of the app; enables auto-configuration, component scanning, and Spring Boot defaults
@EnableAutoConfiguration → Lets Spring configure beans based on classpath dependencies
@ComponentScan → Automatically discovers and registers components in specified packages
🔹 Component & Layered Architecture:
@Component → Generic Spring-managed bean
@Service → Business logic layer (helps readability & AOP support)
@Repository → Data access layer with automatic exception translation
@Controller → Handles web requests (returns views)
@RestController → Builds REST APIs (returns JSON/XML)
🔹 Dependency Injection:
@Autowired → Injects required dependencies automatically
@Qualifier → Resolves ambiguity when multiple beans exist
@Primary → Marks a default bean when multiple implementations are available
🔹 Web & REST APIs:
@RequestMapping → Maps HTTP requests to controller methods
@GetMapping → Handles HTTP GET requests (fetch data)
@PostMapping → Handles HTTP POST requests (create data)
@PutMapping → Handles HTTP PUT requests (update data)
@DeleteMapping → Handles HTTP DELETE requests
@RequestBody → Maps request payload to Java object
@PathVariable → Reads values from URL path
@RequestParam → Reads query parameters
@ResponseStatus → Customizes HTTP response status codes
🔹 Configuration & Bean Management:
@Configuration → Defines configuration classes
@Bean → Explicitly creates and manages a bean
@Value → Injects values from properties files
@ConfigurationProperties → Binds external configs to POJOs
🔹 Database & JPA:
@Entity → Maps Java class to database table
@Id → Marks primary key
@GeneratedValue → Auto-generates primary key values
@Table → Customizes table name and schema
@Column → Maps class fields to DB columns
@Transactional → Ensures atomic DB operations (rollback on failure)
🔹 Validation:
@Valid → Triggers validation on request payload
@NotNull → Ensures field is not null
@NotBlank → Ensures string is not empty or whitespace
@Size → Restricts length of input
@Email → Validates email format
🔹 Exception Handling:
@ExceptionHandler → Handles specific exceptions in controllers
@ControllerAdvice → Centralized exception handling across controllers
@RestControllerAdvice → Exception handling for REST APIs (JSON responses)
🔹 Security:
@EnableWebSecurity → Enables Spring Security configuration
@PreAuthorize → Role/permission-based access control before method execution
@Secured → Restricts access using roles
🔹 Scheduling, Async & Caching:
@Scheduled → Runs tasks at fixed intervals (cron jobs)
@Async → Executes methods asynchronously
@EnableCaching → Enables caching mechanism
@Cacheable → Caches method results
@CacheEvict → Removes cache entries when data changes