24 - Key Spring Boot Annotations Explained
- 23 - Spring Boot Architecture Overview
- 24 - Key Spring Boot Annotations ExplainedCurrent
- 25 - Auto-Configuration: How Spring Boot Does Magic
- 26 - Spring Boot Starters – The Complete Guide
- 27 - Creating a Basic Spring Boot Application (Hands-on)
- 28 - Embedded Web Servers in Spring Boot (Tomcat, Jetty, Netty)
- 29 - Spring Boot Application Startup Process
- 30 - Understanding the main() Method in Spring Boot
- 31 - Best Practices for Spring Boot Applications
- 32 - application.properties vs application.yml in Spring Boot
- 33 - Profiles & Environment Configuration in Spring Boot (@Profile)
- 34 - Spring Boot Logging – Complete Guide
- 35 - Spring Boot DevTools – Hot Reloading & Developer Productivity
- 36 - Using Spring Boot Actuator (Health, Metrics, Insights)
Spring Boot relies heavily on annotations to reduce configuration and make applications concise, readable, and maintainable.
While Spring Core annotations define the foundation, Spring Boot adds its own opinionated, productivity-focused annotations.
This post explains the key Spring Boot annotations you must understand to work effectively with core features.
1. @SpringBootApplication (The Entry Point)
This is the most important annotation in any Spring Boot application.
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
It is a meta-annotation composed of:
@Configuration@EnableAutoConfiguration@ComponentScan
It tells Spring Boot to:
- Start auto-configuration
- Scan components
- Bootstrap the application context
2. @EnableAutoConfiguration
This annotation enables Spring Boot’s auto-configuration mechanism.
What it does:
- Scans the classpath
- Detects available libraries
- Applies conditional configuration
- Registers beans automatically
You almost never use it directly because it’s already included in @SpringBootApplication.
3. @Configuration
Marks a class as a configuration source.
@Configuration
public class AppConfig { }
Use it to:
- Define beans using
@Bean - Group related configuration
- Override auto-configured beans
4. @Bean
Used to create a bean manually.
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
Common use cases:
- Third-party libraries
- Custom initialization logic
- Overriding auto-configured beans
5. Stereotype Annotations
Spring Boot builds on Spring stereotypes to organize layers.
5.1 @Component
Generic component annotation.
@Component
public class UtilityService { }
5.2 @Service
Represents the business logic layer.
@Service
public class OrderService { }
5.3 @Repository
Used for data access layers.
@Repository
public class UserRepository { }
Benefits:
- Semantic clarity
- Automatic exception translation
5.4 @Controller vs @RestController
@Controller
public class HomeController { }
@RestController
public class UserController { }
@Controller→ returns views@RestController→ returns JSON/XML responses
@RestController is a combination of:
@Controller@ResponseBody
6. Dependency Injection Annotations
6.1 Constructor Injection (Recommended)
@Service
public class PaymentService {
private final Gateway gateway;
public PaymentService(Gateway gateway) {
this.gateway = gateway;
}
}
Spring Boot automatically injects dependencies without @Autowired.
6.2 @Autowired
Used to inject dependencies explicitly.
@Autowired
private UserService userService;
Constructor injection is preferred over field injection.
6.3 @Qualifier and @Primary
Used when multiple beans of the same type exist.
@Qualifier("stripeGateway")
@Primary
@Service
public class DefaultGateway { }
7. Web Mapping Annotations
7.1 @RequestMapping
Base request mapping.
@RequestMapping("/api")
7.2 HTTP Method Annotations
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
These map HTTP methods directly.
8. Configuration & Environment Annotations
8.1 @Value
Injects configuration values.
@Value("${server.port}")
private int port;
8.2 @Profile
Activates beans conditionally based on environment.
@Profile("dev")
9. Lifecycle & Utility Annotations
9.1 @PostConstruct & @PreDestroy
Used for initialization and cleanup logic.
9.2 @Transactional
Manages database transactions.
@Transactional
public void placeOrder() { }
10. Best Practices
- Use
@SpringBootApplicationonly once - Prefer constructor injection
- Use stereotypes for layer clarity
- Avoid overusing annotations
- Understand what each annotation actually does
11. Summary
- Annotations are central to Spring Boot
@SpringBootApplicationbootstraps everything- Auto-configuration is enabled implicitly
- Stereotypes define application layers
- Proper usage leads to clean, maintainable code
What's Next?
Next post:
25 - Auto-Configuration: How Spring Boot Does Magic
We’ll deep dive into the internal mechanics behind Spring Boot’s auto-configuration.
