Table of contents
1.
Introduction
2.
@Component Annotation
2.1.
Key Features
2.2.
Syntax
3.
Spring @Component Example
3.1.
Example
4.
@Component
4.1.
Spring Stereotype Annotations
4.2.
@ComponentScan
4.3.
Example
4.4.
Key Benefits
5.
@Component Limitations
6.
@Component vs @Bean
7.
Frequently Asked Questions
7.1.
What is the purpose of the @Component annotation in Spring Boot? 
7.2.
How does @ComponentScan work? 
7.3.
What are some alternatives to @Component? 
8.
Conclusion
Last Updated: Jan 26, 2025
Easy

@Component annotation in Spring Boot

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Spring Boot is a powerful framework used to create Java-based applications. One of its key features is the use of annotations to simplify configuration and setup. The @Component annotation is widely used in Spring Boot to manage and define Spring Beans. 

@Component annotation in Spring Boot

This article will explain the @Component annotation in Spring Boot, its functions, and related concepts like stereotype annotations, @ComponentScan, and limitations. 

@Component Annotation

The @Component annotation in Spring Boot is used to indicate that a class is a Spring-managed bean. When a class is annotated with @Component, it becomes eligible for component scanning, allowing Spring to detect and register the bean automatically during application startup.

Key Features

  • Simplifies the creation of Spring Beans.
     
  • Makes classes discoverable during the component scanning process.
     
  • Works as a foundation for other stereotype annotations like @Service and @Repository.

Syntax

@Component
public class MyComponent {
    public void showMessage() {
        System.out.println("Hello from MyComponent!");
    }
}

In this example, the MyComponent class is registered as a Spring Bean, which can be used throughout the application.

Spring @Component Example

Let’s look at an example of using the @Component annotation in Spring Boot to demonstrate its functionality.

Example

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class ComponentExampleApplication implements CommandLineRunner {

    @Autowired
    private MyComponent myComponent;

    public static void main(String[] args) {
        SpringApplication.run(ComponentExampleApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        myComponent.showMessage();
    }
}

@Component
class MyComponent {
    public void showMessage() {
        System.out.println("Hello from @Component example!");
    }
}


Explanation:

  1. Class Registration: The MyComponent class is annotated with @Component, making it a Spring Bean.
     
  2. Dependency Injection: The @Autowired annotation is used to inject the MyComponent bean into the main application class.
     
  3. Execution: When the application runs, the showMessage method is called, and the message is printed.
     

Output:

Hello from @Component example!

@Component

Spring Stereotype Annotations

Stereotype annotations in Spring are specialized annotations built on top of @Component. These include:

@Service: Used to annotate service-layer classes.

@Service
public class UserService {
    public void processUser() {
        System.out.println("Processing user...");
    }
}


@Repository: Used for Data Access Objects (DAO).

@Repository
public class UserRepository {
    public void saveUser() {
        System.out.println("User saved to database.");
    }
}


@Controller: Used for Spring MVC controllers.

@Controller
public class HomeController {
    @RequestMapping("/home")
    public String home() {
        return "home";
    }
}

 

These annotations enhance readability and convey the role of the class in the application.

@ComponentScan

@ComponentScan is used to specify the base packages for component scanning. By default, Spring scans the package where the main application class resides. You can customize it using the @ComponentScan annotation.

Example

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.example.services")
public class ComponentScanExample {
    public static void main(String[] args) {
        SpringApplication.run(ComponentScanExample.class, args);
    }
}

Explanation:

  • Base Packages: The @ComponentScan annotation specifies that Spring should scan the com.example.services package for components.

Key Benefits

  • Ensures that only the specified packages are scanned, improving application startup performance.

@Component Limitations

While @Component is highly versatile, it has some limitations:

  1. Lack of Specificity:
    • It does not indicate the layer (e.g., service, repository) where the class belongs.
       
    • Use specialized annotations like @Service or @Repository for clarity.
       
  2. Scalability:
    • In large applications, relying solely on @Component can make code harder to manage.
       
  3. Custom Behavior:
    • For complex configurations, additional annotations like @Configuration or @Bean might be more appropriate.

@Component vs @Bean

Parameters@Component@Bean
Definition@Component is a class-level annotation. It marks a Java class as a Spring bean.@Bean is a method-level annotation. It is used to define a bean in a configuration class.
UsageAutomatically detects and registers a class as a bean during component scanning.Used when you need to explicitly define a bean, often for third-party libraries or complex logic.
ControlLess control over bean creation since it relies on Spring’s component scanning.More control over bean creation as you can write custom logic inside the method.
FlexibilityBest for simple beans where no additional configuration is needed.Best for beans that require custom initialization or configuration.
Example@Component
public class UserService {
// class logic
}
@Configuration
public class AppConfig {
@Bean
public UserService userService() {
return new UserService();
}
}

Frequently Asked Questions

What is the purpose of the @Component annotation in Spring Boot? 

The @Component annotation is used to mark a class as a Spring-managed bean, making it eligible for component scanning.

How does @ComponentScan work? 

@ComponentScan specifies the base packages for Spring to scan and register beans automatically.

What are some alternatives to @Component? 

Alternatives include stereotype annotations like @Service, @Repository, and @Controller, which provide better clarity for specific layers in the application.

Conclusion

In this article, we discussed the @Component annotation in Spring Boot, its purpose, and how it simplifies the management of Spring Beans. We discussed related concepts like stereotype annotations, @ComponentScan, and the limitations of @Component. With examples and explanations, you should now feel confident using @Component in your Spring Boot applications.

Live masterclass