0
0
SpringbootComparisonBeginner · 3 min read

@Component vs @Bean: Key Differences and When to Use Each

In Spring, @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
DefinitionClass-level annotation to mark a beanMethod-level annotation to declare a bean
UsagePlaced on classes for automatic detectionPlaced on methods inside @Configuration classes
Bean CreationSpring creates bean by instantiating the classBean created by invoking the annotated method
FlexibilityLess flexible, uses default constructor or autowired dependenciesMore flexible, can customize bean creation logic
Typical Use CaseFor simple components like services, repositoriesFor third-party or complex beans needing custom setup
Requires Configuration ClassNoYes, 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:

java
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:

java
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.
Use @Component for your own simple classes and @Bean for third-party or customized beans.
@Bean requires a class annotated with @Configuration.