Spring Boot @Component Example


1. Introduction

The @Component annotation in Spring Boot is a core principle behind the framework's dependency injection feature. It designates a class as a component, meaning Spring will automatically handle its life cycle and make it available for dependency injection where needed. 

1. Introduction

Spring Boot's @Component annotation is at the heart of Spring's dependency injection mechanism. It marks a Java class as a bean, a candidate for auto-detection when using annotation-based configuration and classpath scanning. In this example, we will explore how to define a MessageSender class as a Spring component and inject a MessageService into it.

Key Points:

1. The @Component annotation allows Spring to automatically detect our custom beans.

2. It simplifies the process of wiring in dependencies, thus promoting loose coupling between classes.

3. Classes annotated with @Component are automatically created as a single instance per Spring ApplicationContext.

4. @Component serves as a specialization of @Bean, but for class-level annotation.

Key Points:

1. @Component is used to auto-detect and auto-configure beans within the Spring context.

2. Dependency injection in Spring can be achieved with @Autowired, which allows Spring to resolve and inject collaborating beans into our bean.

2. Implementation Steps

1. Create a MessageService class annotated with @Component.

2. Create a MessageSender class, also annotated with @Component.

3. Inject the MessageService into the MessageSender using @Autowired.

4. Implement the CommandLineRunner interface in the main application class to test the MessageSender.

5. Run the application and observe the output.

3. Implementation Example

CommandLineRunner is an interface used to indicate that a bean should run when it is contained within a SpringApplication. We use CommandLineRunner to execute some code after the Spring application has started.

Here is the complete code that demonstrates the usage of @Component annotation:

// Step 1: Create a service class to send messages
public class MessageService {

    public String getMessage() {
        return "Hello from MessageService";

// Step 2: Create a sender class to use the message service
public class MessageSender {

    private final MessageService messageService;

    // Step 3: Use constructor injection for the message service
    public MessageSender(MessageService messageService) {
        this.messageService = messageService;

    public void sendMessage() {

// Implement CommandLineRunner to run the message sender after the application context is loaded
public class ComponentExampleApplication implements CommandLineRunner {

    private final MessageSender messageSender;

    // Constructor injection of the message sender
    public ComponentExampleApplication(MessageSender messageSender) {
        this.messageSender = messageSender;

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

    public void run(String... args) throws Exception {
        // Step 4: Use the message sender to send a message


Spring Boot @Component Example


1. @Component is used to annotate the MessageService class, indicating that it is a bean and allowing Spring to manage it.

2. Another class, MessageSender, is created with the @Component annotation, making it a Spring-managed bean as well.

3. @Autowired is used on the constructor of MessageSender, enabling Spring to inject an instance of MessageService into it.

4. The ComponentExampleApplication class implements CommandLineRunner with the run method, which is called after the application has started.

5. Inside the run method, the sendMessage method of the MessageSender bean is called, which in turn uses MessageService to get and print the message.

6. The output shown is the result of MessageSender invoking getMessage on MessageService, demonstrating the successful injection and collaboration between Spring-managed components.