Backend Notes DEC 02, 2025 • 03 MIN READ

WHAT DIFFERENCE
BETWEEN
@BEAN AND @COMPONENT

Abstract technical background

Even experienced Spring developers sometimes mix these two up. Both create beans in the Spring container but they come from different worlds and have different purposes.

Let’s break it down.

@Component — Class-Level, Automatic Bean Discovery

@Component is used on the class itself.

When Spring scans your package (Component Scanning), it finds all classes annotated with:

  • check_circle @Component
  • check_circle @Service
  • check_circle @Repository
  • check_circle @Controller

and registers them automatically as beans.

When to use:

  • check_circle For your own classes
  • check_circle When you control the source code
  • check_circle When the bean requires simple construction
  • check_circle When no complex initialization is needed

Example

TokenGenerator.java JAVA
@Component
public class TokenGenerator {
  public String generate() {
    return UUID.randomUUID().toString();
  }
}

Spring picks it up automatically during component scanning.

@Bean — Method-Level, Manual Bean Creation

@Bean is used inside a class marked with @Configuration.
It gives you full control over how the bean is created.
You write a method, return the object you want to register, and Spring adds it to the container.

When to use:

  • check_circle When you don’t control the class (e.g., third-party library objects)
  • check_circle When the object needs custom initialization, parameters, or logic
  • check_circle For beans that cannot be created via @Component
  • check_circle For conditional beans or advanced wiring

Example

AppConfig.java JAVA
@Configuration
public class AppConfig {
  @Bean
  public ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.findAndRegisterModules();
    return mapper;
  }
}

You manually tell Spring how to build the object.

Summary

@Component vs @Bean

Location:
@Component is placed directly on the class you want Spring to manage, whereas @Bean is used on a method inside a @Configuration class.

Bean Creation:
@Component beans are created automatically during component scanning. In contrast, @Bean requires you to manually define the object inside the method, giving you explicit control over how it’s instantiated.

Use Case:
@Component is ideal for classes you wrote yourself and want Spring to manage. @Bean is better for third-party classes or objects that need complex setup and cannot be annotated directly.

Customization:
@Component offers limited customization, though you can optionally provide a bean name via @Component("name"). @Bean provides full control over how the bean is created, configured, and initialized.

Requires @Configuration?:
@Component does not require a @Configuration class. @Bean methods must reside within a @Configuration class so Spring knows to execute them and register the returned objects as beans.

Bean Name Control:
For @Component, the bean name is optional and can be explicitly defined. For @Bean, the method name automatically becomes the bean name unless you override it.

READ BEYOND THE VOID

Technical blueprint background
Backend Notes

Why Spring Has Three Annotations That Do the Same Thing

If you’ve been working with Spring for even a week, you’ve probably seen annotations like...

Read Entry
Abstract digital network
Backend Notes

The Real Difference Between Spring MVC and WebFlux

Should I use Spring MVC or Spring WebFlux?...

Read Entry
Circuit board macro
Backend Notes

Relation Many to Many with JPA in Spring Boot

I want to create simple service to demo relation many to many...

Read Entry