Spring Framework provides a powerful Dependency Injection (DI) mechanism to manage application components. In this system, objects are managed by an IoC (Inversion of Control) container. But how do we tell Spring which classes or objects it should manage?
There are two common approaches:
@Component
@Bean
Although both serve the same end goal—registering a bean with the Spring container—they work in different ways and are used in different contexts. Let’s explore their differences with examples and best practices.
What is @Component? #
@Component is a class-level annotation that tells Spring to automatically detect and register the class as a bean during component scanning.
import org.springframework.stereotype.Component;
@Component
public class MyService {
public void doSomething() {
System.out.println("Component-based service is working!");
}
}
🔍 Note: Annotations like @Service, @Repository, and @Controller are specializations of @Component. They provide semantic meaning in layered architectures.
To enable component scanning, you need to annotate your configuration class like this:
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
}
What is @Bean? #
@Bean is used to explicitly declare a bean in a method inside a @Configuration class. It’s perfect for registering beans manually or configuring third-party objects.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
Feature | Component | Bean |
---|---|---|
Placement | On the class | On a method |
Registration | Automatic via scanning | Explicit via method |
Flexibility | Less flexible | More flexible |
External Libraries | Not suitable | Suitable |
Dependency Injection | Via constructor or field | Via method parameters |
When Should You Use Each? #
Use Component when: #
-
You control the class source code.
-
You want automatic discovery and registration.
-
You’re organizing your app into layers (@Service, @Repository, etc.).
Use Bean when: #
-
You’re working with external libraries or third-party classes.
-
You need to manually configure or customize the object.
-
You want fine-grained control over bean creation.
Conclusion #
Both @Component and @Bean are essential tools for defining Spring beans, but they shine in different scenarios.
-
Use @Component for classes you write and control.
-
Use @Bean for manual configuration, third-party classes, or when you need full control over instantiation.
Understanding the strengths and use-cases of each will help you write cleaner, more maintainable Spring applications.