Explain @ComponentScan annotation
The `@ComponentScan` annotation is used in Spring to automatically detect and register beans within the Spring application context. It is typically used along with `@Configuration` and `@SpringBootApplication` annotations to specify the base packages to scan for annotated components (e.g., `@Component`, `@Service`, `@Repository`, `@Controller`).
By default, `@ComponentScan` scans the package of the class that declares this annotation. However, you can specify a different base package to scan by providing a `basePackages` attribute.
Table of Contents
Example
Step 1: Create a Component
Define a simple Spring component using the `@Component` annotation.
```java
package com.example.service;
import org.springframework.stereotype.Component;
@Component
public class MyComponent {
public String getMessage() {
return "Hello from MyComponent!";
}
}
```
Explanation:
@Component
: Marks the class as a Spring component, making it eligible for component scanning and registration as a Spring bean.
Step 2: Create a Configuration Class with @ComponentScan
Create a configuration class that uses the @ComponentScan
annotation to specify the base package to scan for components.
```java
package com.example.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.example.service")
public class AppConfig {
}
```
Explanation:
@Configuration
: Indicates that the class can be used by the Spring IoC container as a source of bean definitions.@ComponentScan(basePackages = "com.example.service")
: Specifies the base package to scan for Spring components.
Step 3: Using the Component in Application
In your main application class or any other class, you can now inject the scanned component.
```java
package com.example.demo;
import com.example.service.MyComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private MyComponent myComponent;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println(myComponent.getMessage());
}
}
```
Explanation:
@Autowired
: Used to inject theMyComponent
bean that was scanned and registered by the@ComponentScan
annotation.- The
run
method prints the message returned by thegetMessage
method ofMyComponent
.
Conclusion
- @ComponentScan: Used to specify the base packages to scan for annotated Spring components. It helps in automatically detecting and registering beans within the Spring application context.
- Example: Demonstrated how to use
@ComponentScan
to scan for components and inject them into a Spring Boot application.