Skip to main content
  1. Posts/

Component vs Bean in Spring

2 mins· loading · loading ·
Technology Java Programming Software Development Comparison
Table of Contents

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

  1. You control the class source code.

  2. You want automatic discovery and registration.

  3. You’re organizing your app into layers (@Service, @Repository, etc.).

Use Bean when:
#

  1. You’re working with external libraries or third-party classes.

  2. You need to manually configure or customize the object.

  3. 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.

  1. Use @Component for classes you write and control.

  2. 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.

Related

Java vs C#
5 mins· loading · loading
Technology Programming C# Java Software Development Comparison
Why I Like Java More Than Python
4 mins· loading · loading
Technology Java Python Programming Software Development Comparison
🎮 The 20 Most Played Games on Steam (August 2025)
4 mins· loading · loading
Gaming Steam PC Gaming Multiplayer Top Games Gaming Trends
🎮 Top 10 Best Action Games on Steam
3 mins· loading · loading
Gaming Action Games Steam PC Gaming Top 10 Gaming
🎮 Top 10 Best Strategy Games on Steam
4 mins· loading · loading
Gaming Strategy Games Steam PC Gaming Top 10 Gaming