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:
@SpringBootApplication
// Equals to:
@Configuration
@EnableAutoConfiguration
@ComponentScan
π Use it on your main class
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
@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).
@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:
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
@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.