Difference Between Bean and Component in Spring Boot

Bean and Component are both Spring Beans in Spring Boot. To create these by using the annotations(@Bean and @Component). This topic explains the difference between Bean and Component and how they impact the configuration of a Spring Boot application. 

Difference Between Bean and Component

What is Component in Spring Boot?

It is a Java Bean class and annotated(@Component) annotation in Spring Boot.

Key Characteristics

  • It declares a class as a Spring-managed bean.
  • It is automatically discovered through classpath scanning.
  • It works with derived stereotypes like @Service, @Repository, @Controller.

Example:

@Component

public class EmailService {

    public void sendEmail(String to, String message) {

        System.out.println("Sending email to " + to);

    }

}

What is Bean in Spring Boot?

A Java Bean class to configure as a Spring Bean, we need to use @Bean in the @Configuration annotated class. 

Key Characteristics

  • Used to define beans outside the class itself.

Example:

@Configuration

public class AppConfig {

    @Bean

    public EmailService emailService() {

        return new EmailService(); // manually instantiated

    }

}

Key Difference Between Bean and Component

FeatureComponentBean
Annotation TypeClass-levelMethod-level
Bean RegistrationAutomatically detected via component scanningManually defined in @Configuration class
Use CaseApplication code you write and controlThird-party classes or conditional bean creation
Configuration LocationOn the class itselfInside a class annotated with @Configuration
Control Over InstantiationLimited (Spring creates the object)Full control (you return the bean yourself)
FlexibilitySimple and quickHighly customizable

When to Use @Component vs @Bean

Use @Component When

  • We are writing our own classes (services, components, controllers).
  • We want to automatically detect beans during startup.

Use @Bean When

  • When we want to create beans from third-party classes, like from a library.
  • We want more control over bean instantiation(setting constructor arguments manually).
  • We need to conditionally register beans based on environment or logic.

Can we Use Both Together?

Yes, we can use both Bean and Component Spring Bean classes in the same Spring Boot project, depending on your needs.

Example

  • Use @Component for our app’s business classes.
  • Use @Bean for external beans or custom configurations.

Real-World Scenario

Using @Component

@Component

public class SmsService {

    public void send(String phone, String message) {

        System.out.println("Sending SMS to " + phone);

    }

}

Using @Bean

@Configuration

public class AppConfig {

    @Bean

    public DateFormatter dateFormatter() {

        return new DateFormatter("yyyy-MM-dd");

    }

}
  • SmsService is automatically detected and injected.
  • DateFormatter is a third-party or utility bean manually declared.

Conclusion

In Spring Boot:

@Component is for auto-detecting and registering our classes.

@Bean is for explicitly declaring beans, especially for external or utility classes.

✅ Use @Component for simplicity and convention.

✅ Use @Bean for flexibility and full control.

Understanding this difference helps us structure our code cleanly and maintain a scalable Spring Boot application

Leave a Comment