Junior — Middle
What is the role of the ComponentScan annotation in Spring application configuration?
sobes.tech AI
Answer from AI
The @ComponentScan annotation in Spring indicates to the framework which packages to scan for classes annotated with component annotations (@Component, @Service, @Repository, @Controller, etc.) in order to automatically register them as beans in the application context.
Without @ComponentScan, Spring will not know where to look for these classes, and they will not be created and injected automatically.
Example of usage:
@Configuration
@ComponentScan(basePackages = "com.example.app")
public class AppConfig {
// Spring configuration
}
Here, Spring will scan the com.example.app package and its subpackages for components.