The Purpose of using @ComponentScan
The @ComponentScan` annotation is used in Spring Framework to specify the base packages to scan for annotated components. By default, Spring uses `@Component Scan` to look for components in the package where the application is defined and its sub-packages. This helps in automatically discovering and registering beans defined with annotations such as `@Component`, `@Service`, `@Repository`, and `@Controller`.
Key Purposes of `@ComponentScan`:
- Automatic Bean Detection: Helps Spring automatically discover and register beans without requiring manual configuration.
- Modularization: Allows you to specify which packages to scan, promoting modularity and separation of concerns.
- Custom Scanning: Provides flexibility to scan specific packages or exclude certain classes from the scanning process.
Table of Contents
Example
Example Usage
1. Basic Usage of `@ComponentScan`
When you use `@ComponentScan`, you typically apply it to the main configuration class of your Spring application.
Example Main Application Class:
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Component Scan;
@SpringBootApplication
@ComponentScan(basePackages = "com.example.demo.services")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
Explanation:
@SpringBootApplication
includes@Component Scan
with a default base package, but you can override this using the@Component Scan
annotation.basePackages
specifies the package to scan for annotated components. In this case, it scans thecom.example.demo.services
package.
Example
2. Using `@Component Scan` with Multiple Packages
You can specify multiple base packages if your components are scattered across different packages.
Example:
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.example.demo.services", "com.example.demo.controllers"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
Explanation:
- Here,
@Component Scan
is configured to scan bothcom.example.demo.services
andcom.example.demo.controllers
packages.
Example
3. Excluding Specific Classes from Component Scanning
You can exclude specific classes or components from being scanned using `excludeFilters`.
Example:
```java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Component Scan;
import org.springframework.context.annotation.FilterType;
@SpringBootApplication
@ComponentScan(basePackages = "com.example.demo",
excludeFilters = @Component Scan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = UnwantedService.class))
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
Explanation
excludeFilters
is used to exclude specific classes from component scanning. In this example,UnwantedService.class
will not be scanned or registered as a bean.
Conclusion
- Purpose of
@Component-Scan
: - Automatic Bean Detection: Automatically discovers and registers beans.
- Modularization: Allows specific packages to be scanned, promoting modular design.
- Custom Scanning: Offers flexibility to include or exclude certain packages or classes.
- Usage Examples:
- Basic Usage: Scans a specified package.
- Multiple Packages: Scans multiple specified packages.
- Exclusion: Excludes specific classes from scanning.