Spring Boot provides a rich set of annotations that simplify configuration, dependency injection, web development, data access, and application management. These annotations reduce boilerplate code and help developers build applications quickly using convention over configuration.
Key Points: • Spring Boot annotations automate configuration and bean management. • They promote clean architecture by separating responsibilities across layers. • Most Spring Boot applications rely heavily on annotation-based configuration instead of XML.
Example: In a typical Spring Boot application:
• Controller handles HTTP requests. • Service contains business logic. • Repository interacts with the database.
Spring annotations help identify and manage each layer automatically.
Common Spring Boot Annotations:
1. @SpringBootApplication
Purpose: • Main entry point of a Spring Boot application.
Internally combines: • @Configuration • @EnableAutoConfiguration • @ComponentScan
Example:
@SpringBootApplication
public class Application {
}2. @RestController
Purpose: • Marks a class as a REST API controller. • Combines @Controller and @ResponseBody.
3. @RequestMapping
Purpose: • Maps HTTP requests to controller methods.
Related annotations: • @GetMapping • @PostMapping • @PutMapping • @DeleteMapping
4. @Service
Purpose: • Identifies business logic classes.
Example: • UserService • PaymentService
5. @Repository
Purpose: • Identifies data access layer components. • Enables automatic exception translation.
6. @Component
Purpose: • Registers a generic Spring-managed bean.
7. @Autowired
Purpose: • Performs dependency injection automatically.
Preferred usage: • Constructor Injection
8. @Configuration
Purpose: • Defines configuration classes and bean definitions.
9. @Bean
Purpose: • Creates and manages custom beans explicitly.
10. @Value
Purpose: • Injects values from properties or environment variables.
Example:
@Value("${server.port}")
private String port;11. @Profile
Purpose: • Loads beans only for specific environments.
Examples: • dev • test • prod
12. @Transactional
Purpose: • Manages database transactions automatically.
13. @EnableAsync
Purpose: • Enables asynchronous method execution using @Async.
14. @EnableScheduling
Purpose: • Enables scheduled tasks using @Scheduled.
15. @ConfigurationProperties
Purpose: • Maps configuration properties to Java objects.
Layer-Wise Annotation Usage:
Presentation Layer: • @RestController • @RequestMapping
Business Layer: • @Service
Persistence Layer: • @Repository
Configuration Layer: • @Configuration • @Bean
Dependency Injection: • @Autowired
Real-World Example:
E-Commerce Application:
OrderController ↓ OrderService ↓ OrderRepository ↓ Database
Annotations automatically wire these components together with minimal configuration.
Interview Tip: A concise interview answer is: Some of the most commonly used Spring Boot annotations are @SpringBootApplication, @RestController, @RequestMapping, @Service, @Repository, @Component, @Autowired, @Configuration, @Bean, and @Transactional. These annotations simplify configuration, dependency injection, web development, and data access while significantly reducing boilerplate code.