@Component Annotation — When and How to Use It
Fast recall
Quick revision
Core answer
Understanding when @Component is the appropriate choice versus specialized stereotypes like @Service or @Repository
Using @Component everywhere instead of semantically appropriate annotations, or not knowing what types of classes qualify as generic components
Provide concrete examples like ApplicationRunner, scheduled tasks, custom validators, or event listeners—showing you understand component stereotypes map to architectural layers
Interview-ready guide
Interview answer
@Component is Spring's catch-all stereotype annotation.
It tells the framework to register the class as a bean during component scanning. So when your Spring Boot app starts, it looks for @Component classes, creates instances, and makes them available for dependency injection. You use @Component for classes that don't fit the typical layers.
Let me give you concrete examples. An event listener that reacts to domain events like OrderPlacedEvent—that's not a service, it's infrastructure, so you'd use @Component. Or a scheduled task that runs every hour to clean up expired sessions—it needs access to a repository, but it's not business logic, so @Component fits.
Same with custom validators, type converters, or ApplicationRunner implementations that do startup tasks. The key mistake Learners make is using @Component for everything. If your class does business logic, use @Service.
If it talks to the database, use @Repository. If it handles web requests, use @Controller. @Component is the fallback when none of those fit.
This way your code documents its own architecture—someone reading @Service immediately knows what layer they're in.
@Component Annotation — When and How to Use It
Direct Answer
@Component is Spring's catch-all stereotype annotation that marks a class as a Spring-managed bean. The framework detects it during component scanning and registers the class in the application context. Use @Component for classes that don't fit the service, repository, or controller layers—like event listeners, schedulers, validators, converters, or utility classes needing dependency injection.
Code Example
@Component
public class OrderEventListener {
private final NotificationService notificationService;
public OrderEventListener(NotificationService notificationService) {
this.notificationService = notificationService;
}
@EventListener // Spring detects this because class is a @Component
public void handleOrderPlaced(OrderPlacedEvent event) {
notificationService.sendConfirmation(event.getOrderId());
}
}
@Component
public class DataInitializer implements ApplicationRunner {
private final UserRepository userRepository;
public DataInitializer(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public void run(ApplicationArguments args) {
// Runs once at startup, needs dependency injection
if (userRepository.count() == 0) {
userRepository.save(new User("admin", "admin@example.com"));
}
}
}
Common Mistakes & Pitfalls
- Using @Component for everything: Learners often annotate all classes with @Component instead of using semantically meaningful stereotypes like @Service or @Repository. This loses documentation value—when you see @Service, you know it's business logic; @Component gives no hint. Spring Boot tooling and AOP can also apply layer-specific behavior to specialized stereotypes.
- Forgetting @Component on classes needing injection: If your event listener or validator isn't annotated, Spring won't detect it during component scanning, and the class won't be a bean—leading to NullPointerExceptions when you try to autowire it.
- Not understanding component scan base packages: Placing @Component classes outside your main application package means they won't be found unless you explicitly configure @ComponentScan with additional packages.
Check your understanding
Answer these without looking back at the article.
- What is ApplicationRunner vs CommandLineRunner?
- How does @Scheduled work?
- What is the difference between @Component and @Bean for the same class?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.