Spring Framework FAQ: Top Questions
47. What is the role of @ComponentScan in Spring?
@ComponentScan
tells Spring where to look for components, configurations, and services. It scans the specified packages for classes annotated with @Component
, @Service
, @Repository
, etc.
📥 Example:
@Configuration
@ComponentScan(basePackages = "com.example.app")
public class AppConfig {
}
🏆 Expected Output:
Spring scans and registers beans under the specified package.
🛠️ Use Cases:
- Enable automatic component detection and registration.
- Split application into logical modules.