What is the difference between @Bean and @Component in Spring?
Learn the difference between @Bean and @Component in Spring, when to use each, how scanning works, and why @Bean is needed for third-party classes.
Expected Interview Answer
@Component marks a class for automatic detection through classpath scanning, while @Bean is a method-level annotation inside a @Configuration class that explicitly tells Spring how to create and register a bean.
@Component (and its stereotypes @Service, @Repository, @Controller) is placed on your own classes so component scanning instantiates them automatically. @Bean is used when you need explicit control over construction, or when the type is a third-party class you cannot annotate, such as a RestTemplate or a DataSource. With @Bean you write the factory method body yourself and can configure the object, wire dependencies through method parameters, and choose the bean name, giving finer control than convention-based scanning.
- @Component enables fast, convention-based auto-detection of your own classes
- @Bean registers third-party classes you cannot annotate
- @Bean gives explicit, programmatic control over object construction
- Stereotype variants of @Component add semantic meaning
- Both integrate into the same application context and dependency injection
AI Mentor Explanation
@Component is like a selector auto-picking every player who shows up in the registered squad list based on their tagged position. @Bean is the captain manually assembling a specific playing eleven, deciding exactly how each slot is filled, including a guest specialist borrowed from another team who was never on the squad sheet.
Step-by-Step Explanation
Step 1
Decide ownership of the class
If you own the class and can annotate it, prefer @Component for auto-detection.
Step 2
Handle third-party types
For classes you cannot modify, declare a @Bean method in a @Configuration class.
Step 3
Enable scanning
@ComponentScan (implicit in Spring Boot) finds @Component classes on the classpath.
Step 4
Write the factory method
For @Bean, return a fully constructed and configured object from the method body.
Step 5
Let the container wire
Both bean types join the ApplicationContext and are injected wherever their type is required.
What Interviewer Expects
- @Component is class-level and auto-detected by scanning
- @Bean is method-level and explicitly defined in @Configuration
- @Bean is used for third-party or externally-constructed objects
- Awareness of stereotypes like @Service and @Repository
- Both end up as beans in the same application context
Common Mistakes
- Saying they are interchangeable in every situation
- Trying to put @Component on a third-party class you cannot edit
- Forgetting @Bean methods must live in a @Configuration class
- Not knowing @Service and @Repository are specialized @Component
- Assuming @Bean cannot receive dependencies as method parameters
Best Answer (HR Friendly)
“@Component tells Spring to automatically find and create an object from one of your own classes. @Bean is used inside a configuration class when you want to build the object yourself, which is essential for third-party classes you cannot annotate. Both end up managed by Spring.”
Code Example
@Component
public class OrderService {
// Auto-detected by component scanning and registered as a bean
}@Configuration
public class AppConfig {
@Bean
RestTemplate restTemplate() {
// RestTemplate is not our class, so we build and register it explicitly
return new RestTemplate();
}
}Follow-up Questions
- Why can't you use @Component on a class from a third-party library?
- What are the stereotype annotations derived from @Component?
- How does a @Bean method receive its dependencies?
- What is the default bean name for @Component versus @Bean?
- When would you prefer @Bean over @Component for your own class?
MCQ Practice
1. Where must a @Bean method be declared?
@Bean methods are declared inside a @Configuration class so the container can invoke them to produce beans.
2. Which is the best use case for @Bean over @Component?
@Bean lets you explicitly construct and register classes you cannot edit, such as RestTemplate or DataSource.
3. Which annotation is NOT a specialization of @Component?
@Bean is a method-level annotation, not a stereotype; @Service, @Repository, and @Controller are specialized @Component.
Flash Cards
@Component vs @Bean level? — @Component is class-level and auto-scanned; @Bean is method-level and explicitly declared.
When to use @Bean? — When you need explicit construction control or must register a third-party class you cannot annotate.
Where do @Bean methods live? — Inside a @Configuration class.
Name @Component stereotypes. — @Service, @Repository, and @Controller.
Do both become Spring beans? — Yes, both are registered in the same ApplicationContext and available for injection.
Continue Learning
Related Interview Questions
What is auto-configuration in Spring Boot and how does it work?
medium
What is Spring Boot and how does it differ from the Spring Framework?
easy
What is the difference between @Component, @Service, @Repository, and @Controller in Spring?
easy
What is dependency injection in Spring and how does the IoC container work?
medium