Spring Boot @ComponentScan Example

1. Introduction

In Spring Boot, @ComponentScan is used to specify the packages to scan for components, configurations, and services. It is a crucial annotation that tells Spring where to look for annotated classes that will be auto-registered as beans in the Spring application context.

Key Points:

1. @ComponentScan automatically detects and registers beans from specified packages.

2. It is usually used with @Configuration to scan for components in the same and sub-packages.

3. It can be customized with attributes such as basePackages or basePackageClasses to fine-tune scanning.

2. Implementation Steps

1. Define a package structure for your Spring Boot application.

2. Create classes annotated with @Component, @Service, @Repository, or @Controller.

3. Add a @Configuration class with @ComponentScan to specify the packages.

4. Inject components using @Autowired in your application.

5. Run the application to see auto-detection and registration in action.

3. Implementation Example

Here is the complete code that demonstrates the usage of @ComponentScan annotation:
// Define a service component in a custom package
package com.example.myapp.service;

@Component
public class MyService {
    public String getMessage() {
        return "Hello from MyService";
    }
}

// Define a configuration class with @ComponentScan
package com.example.myapp.config;

@Configuration
@ComponentScan(basePackages = "com.example.myapp.service")
public class AppConfig {
    // Additional configurations if needed
}

// Define a Spring Boot application class in a base package
package com.example.myapp;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        // Step 4: Get the bean from the context and call its method
        MyService myService = ctx.getBean(MyService.class);
        System.out.println(myService.getMessage());
    }
}

// Run the Application class as a Java application.

Output:

Hello from MyService

Explanation:

1. MyService is a standard Spring component defined in the com.example.myapp.service package.

2. AppConfig is annotated with @Configuration and @ComponentScan. It tells Spring to scan the com.example.myapp.service package for components.

3. Application is the main class marked with @SpringBootApplication. Spring Boot's auto-configuration is likely to include @ComponentScan implicitly for the package of the main class and its sub-packages. However, the explicit @ComponentScan in AppConfig is specifically instructing Spring to scan com.example.myapp.service.

4. When running the main application, Spring Boot starts and creates an application context that scans for Spring components. It detects MyService and registers it as a bean.

5. The main method then retrieves MyService from the application context and calls its getMessage() method.

6. The output shows the message from MyService, verifying that the @ComponentScan has successfully registered MyService as a Spring bean.


Comments