@Bean vs @Component in Spring Boot

Spring’s @Component and @Bean annotations both register objects with the Spring IoC container, but they are used in different places.

  • @Component is placed on a class. Component scanning discovers the class and Spring creates and manages an instance.
  • @Bean is placed on a method, normally inside a @Configuration class. Spring invokes the method and manages the object it returns.

That distinction is more accurate than saying one annotation is for your code and the other is for third-party code. You can use @Bean for your own classes, and @Component is normally used on classes whose source code you control.

When to use Spring @Component

Use @Component when a class naturally belongs to your application and can be discovered through component scanning.

import org.springframework.stereotype.Component;

@Component
public class Score {

    private int wins;
    private int losses;
    private int ties;

    public void win() {
        wins++;
    }

    public int wins() {
        return wins;
    }
}

Spring Boot’s @SpringBootApplication includes component scanning. If Score is in the application’s scan path, Spring discovers it and registers a Score bean automatically.

Spring also provides specialized component stereotypes such as @Service, @Repository and @Controller. They are themselves forms of @Component and communicate the role of the class more clearly.

When to use Spring @Bean

Use @Bean when object construction belongs in configuration code, when creation requires explicit setup, or when the class comes from a library and you cannot annotate it with @Component.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ApplicationConfig {

    @Bean
    HikariDataSource dataSource() {
        var dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:h2:mem:roshambo");
        return dataSource;
    }

    @Bean
    ObjectMapper objectMapper() {
        return new ObjectMapper();
    }
}

Here, HikariDataSource and ObjectMapper are third-party classes. You cannot add @Component to their source code, so factory methods annotated with @Bean are a natural way to make those objects Spring-managed.

The original version of this example had the annotations reversed: @Bean cannot annotate the Score class, and @Component cannot annotate the dataSource() or objectMapper() factory methods.

Dependency injection works the same afterward

Once an object is registered, other Spring beans generally do not care whether it came from component scanning or a @Bean method. Constructor injection works the same way.

import org.springframework.stereotype.Service;

@Service
public class GameService {

    private final Score score;

    public GameService(Score score) {
        this.score = score;
    }

    public void recordWin() {
        score.win();
    }
}

@Bean vs @Component rule of thumb

Use @Component, or a more specific stereotype such as @Service or @Repository, for application classes that Spring should discover automatically. Use @Bean when you want explicit construction logic in configuration, especially for third-party objects.

Neither annotation is inherently better for large or small applications. The choice should reflect how the object is created and how clearly the configuration communicates the application’s design.


Cameron McKenzie

Cameron McKenzie is an AWS Certified AI Practitioner, Machine Learning Engineer, Solutions Architect and author of many popular books in the software development and Cloud Computing space. His growing YouTube channel training devs in Java, Spring, AI and ML has well over 30,000 subscribers.