What is Component Scanning in Spring Boot?
Component Scanning is a process in Spring Framework where Spring automatically scans packages to find Spring Components and create Spring Beans.
It eliminates the need for manual bean configuration.
Spring automatically detects classes annotated with:
- @Component
- @Service
- @Repository
- @Controller
- @RestController
Simple Definition
Component Scanning is the process of automatically scanning packages and registering Spring Beans in the IoC Container.
Component Scanning = Automatic bean discovery process in Spring.
Why Component Scanning was Introduced?
Before annotations and component scanning, developers manually configured beans in XML files.
Old XML Configuration
<bean id="paymentService"
class="com.dhanish.service.PaymentService">
</bean>
Large applications required hundreds of XML configurations.
Spring introduced Component Scanning to simplify bean registration.
How Component Scanning Works
During application startup:
- Spring scans packages.
- Detects component annotations.
- Creates Spring Beans.
- Stores beans in IoC Container.
- Dependencies become available for injection.
Main Annotation Used
@ComponentScan
This annotation enables component scanning in Spring.
Basic Example
@ComponentScan
@Configuration
public class AppConfig {
}
Spring scans the current package and sub-packages automatically.
Component Scanning in Spring Boot
In Spring Boot:
@SpringBootApplication
already contains:
@ComponentScan
So component scanning is enabled automatically.
Annotations Detected During Component Scanning
| Annotation | Purpose |
|---|---|
@Component |
Generic Spring Bean. |
@Service |
Business logic layer. |
@Repository |
Database layer. |
@Controller |
MVC Controller. |
@RestController |
REST API Controller. |
Real-Time Example
Suppose we are building Dhanish Empower.
Project Structure:
com.dhanish
├── controller
├── service
├── repository
├── util
└── config
Spring scans these packages automatically.
Example of @Service Detection
@Service
public class PaymentService {
}
Component Scanning detects:
- @Service annotation
- Creates Bean automatically
- Registers Bean inside IoC Container
Example of Dependency Injection
@RestController
public class PaymentController {
@Autowired
private PaymentService paymentService;
}
Because of Component Scanning:
- PaymentService Bean already exists.
- Spring injects it automatically.
Default Package Scanning Rule
Spring Boot scans:
- Current package
- All sub-packages
Example:
com.dhanish
Spring scans everything inside:
com.dhanish.*
Important Best Practice
Main application class should be placed in the root package.
Correct Structure
com.dhanish
├── DhanishEmpowerApplication
├── controller
├── service
└── repository
This ensures all packages are scanned properly.
Custom Package Scanning
We can manually specify packages:
@ComponentScan(basePackages = {
"com.dhanish.service",
"com.dhanish.repository"
})
public class AppConfig {
}
What Happens if Bean is Outside Scan Package?
Spring cannot detect the Bean.
Result:
NoSuchBeanDefinitionException
Internal Flow of Component Scanning
Application Starts
↓
@ComponentScan Activated
↓
Packages Scanned
↓
Annotations Detected
↓
Beans Created
↓
Beans Registered in IoC Container
↓
Dependency Injection Enabled
Advantages of Component Scanning
- Reduces XML configuration
- Automatic bean registration
- Improves developer productivity
- Simplifies application setup
- Supports large-scale applications
- Cleaner architecture
- Easy maintenance
Production-Level Importance
Component Scanning is extremely important in:
- Microservices
- REST APIs
- Enterprise applications
- Cloud-native applications
- Banking systems
It automates bean management in large projects.
Interview Answer
Component Scanning is a process in Spring Framework where Spring automatically scans packages, detects component annotations, and creates Spring Beans automatically.
It is enabled using:
@ComponentScan
Spring Boot automatically enables it through:
@SpringBootApplication
Short Interview Answer
Component Scanning automatically scans packages and creates Spring Beans based on annotations.
Frequently Asked Questions
Which annotation enables Component Scanning?
@ComponentScan
Does @SpringBootApplication include Component Scanning?
Yes.
What annotations are detected during scanning?
@Component, @Service, @Repository, @Controller, and @RestController.
What happens if package is not scanned?
Spring cannot create the Bean.
Why should main class be in root package?
To ensure all sub-packages are scanned properly.
Conclusion
Component Scanning is one of the most important features in Spring Framework and Spring Boot.
It automates bean creation and registration, reduces XML configuration, and simplifies enterprise application development.
It plays a major role in Dependency Injection and Spring IoC Container management.