Spring Boot is heavily annotation-driven. Annotations make your code cleaner, shorter, and easier to read by replacing XML configurations.
They are part of what makes Spring Boot “convention over configuration” — giving you smart defaults with just a few annotations.
In this post, we’ll explore the Key Spring Boot Annotations:
@SpringBootApplication@Component@Configuration

🚀 1. @SpringBootApplication
This is the starting point of any Spring Boot application.
🔍 What it does
It’s a meta-annotation, combining three core Spring annotations:
|
1 2 3 4 5 6 7 |
@SpringBootApplication // Equals to: @Configuration @EnableAutoConfiguration @ComponentScan |
📌 Use it on your main class
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.kscodes.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } |
✅ What it enables
- Registers Beans (
@Configuration) - Triggers auto-configuration (
@EnableAutoConfiguration) - Scans components in the package (
@ComponentScan)
📘 Tip: Place your @SpringBootApplication class at the root of your package to ensure all components are detected properly.
🧩 2. @Component
Marks a class as a Spring-managed bean.
🎯 Purpose
Tells Spring to automatically detect and register the class during component scanning.
📌 Example
|
1 2 3 4 5 6 7 8 9 |
@Component public class MyService { public String greet() { return "Hello from MyService!"; } } |
Spring will now manage MyService and inject it wherever needed.
📘 Tip
There are specialized versions of @Component:
@Service→ for service classes@Repository→ for DAO/data access layers@Controller→ for Spring MVC controllers
All of these are technically @Component under the hood.
🛠️ 3. @Configuration
Defines a class as a source of bean definitions.
🎯 Use case
When you want to declare beans manually (instead of annotating with @Component).
|
1 2 3 4 5 6 7 8 9 10 |
@Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } } |
Now Spring will register MyService as a bean using this configuration.
✅ Use it for:
- Registering beans conditionally
- External library configuration
- Managing complex bean initialization
🔍 How They Work Together
| Annotation | Purpose |
|---|---|
@SpringBootApplication | Bootstraps the whole app |
@Component | Registers a class as a bean |
@Configuration | Declares beans via Java config |
These annotations are at the core of every Spring Boot application.
🧠 Bonus: How Spring Finds and Registers Beans
Spring uses component scanning to look for:
- Classes annotated with
@Component(and its variations) - Classes in the same or sub-packages of the main class
So if your structure is like:
|
1 2 3 4 5 6 7 |
com.kscodes.springboot ├── MyApp.java (@SpringBootApplication) ├── service/ │ └── MyService.java (@Service) ├── config/ │ └── AppConfig.java (@Configuration) |
Spring will find everything automatically 🎯
❗ Common Mistakes to Avoid
| Mistake | Why It’s Problematic |
|---|---|
Placing @SpringBootApplication too deep | Misses components in higher-level packages |
Forgetting @Component or @Service | Class won’t be registered or injected |
| Not annotating config class | Beans won’t be registered without @Configuration |
🧪 Simple Example: Putting It All Together
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
@SpringBootApplication public class DemoApp { public static void main(String[] args) { SpringApplication.run(DemoApp.class, args); } } @Component class GreetingService { public String greet() { return "Hi from GreetingService!"; } } @RestController class GreetingController { private final GreetingService service; public GreetingController(GreetingService service) { this.service = service; } @GetMapping("/hello") public String hello() { return service.greet(); } } |
✅ Summary
| Annotation | What It Does |
|---|---|
@SpringBootApplication | Bootstraps app with default config |
@Component | Registers class as Spring-managed bean |
@Configuration | Used to manually define beans |
These annotations are essential to understanding how Spring Boot works behind the scenes. Mastering them lays the foundation for more advanced topics like dependency injection, bean lifecycle, and auto-configuration.
🔗 External References
- Spring Boot Main Annotations – Official Docs
📘 https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.developing-auto-configuration
Detailed reference for auto-configuration and annotations. - Spring Framework Core Annotations Overview
📘 https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-annotation-config
Explanation of@Component,@Configuration, and related annotations.