@Component vs @Bean: Key Differences and When to Use Each
@Component is a class-level annotation used to mark a class as a Spring-managed bean automatically detected via classpath scanning. @Bean is a method-level annotation used inside configuration classes to explicitly declare a bean instance returned by the method.Quick Comparison
This table summarizes the main differences between @Component and @Bean in Spring.
| Aspect | @Component | @Bean |
|---|---|---|
| Definition | Class-level annotation to mark a bean | Method-level annotation to declare a bean |
| Usage | Placed on classes for automatic detection | Placed on methods inside @Configuration classes |
| Bean Creation | Spring creates bean by instantiating the class | Bean created by invoking the annotated method |
| Flexibility | Less flexible, uses default constructor or autowired dependencies | More flexible, can customize bean creation logic |
| Typical Use Case | For simple components like services, repositories | For third-party or complex beans needing custom setup |
| Requires Configuration Class | No | Yes, must be inside a class annotated with @Configuration |
Key Differences
@Component is a stereotype annotation that marks a class as a candidate for auto-detection when using annotation-based configuration and classpath scanning. Spring automatically detects these classes and registers them as beans in the application context. This approach is simple and works well for your own classes where you can add annotations directly.
On the other hand, @Bean is used on methods inside a class annotated with @Configuration. It explicitly declares a single bean, and the method's return value is registered as a Spring bean. This gives you full control over the bean creation process, allowing you to call constructors with parameters, configure properties, or even create beans from third-party classes that you cannot annotate.
While @Component relies on classpath scanning and is less flexible, @Bean allows for complex initialization logic and is ideal when you need to customize bean creation or integrate external libraries. Both annotations contribute to Spring's dependency injection but serve different purposes and use cases.
Code Comparison
Here is how you define a simple service bean using @Component:
import org.springframework.stereotype.Component; @Component public class MyService { public String greet() { return "Hello from @Component!"; } }
@Bean Equivalent
The same service bean defined using @Bean inside a configuration class:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public MyService myService() { return new MyService(); } } class MyService { public String greet() { return "Hello from @Bean!"; } }
When to Use Which
Choose @Component when: You control the source code of the class and want Spring to automatically detect and manage it as a bean with minimal configuration. It is ideal for simple services, repositories, or controllers.
Choose @Bean when: You need to create beans from third-party classes or require custom initialization logic that cannot be done with simple annotations. It is also preferred when you want explicit control over bean creation inside configuration classes.
Key Takeaways
@Component marks a class for automatic bean detection via scanning.@Bean declares a bean explicitly via a method in a configuration class.@Bean offers more control and flexibility for complex bean creation.@Component for your own simple classes and @Bean for third-party or customized beans.@Bean requires a class annotated with @Configuration.