Junior — Middle
Can you explain what the ComponentScan annotation in the Spring Framework context is?
sobes.tech AI
Answer from AI
The @ComponentScan annotation in the Spring Framework is used for automatic detection and registration of components (beans) in the application context. It specifies which packages Spring should scan to find classes annotated with @Component, @Service, @Repository, @Controller, and others, in order to instantiate and manage them.
Example of usage:
@Configuration
@ComponentScan(basePackages = "com.example.project")
public class AppConfig {
// Spring configuration
}
In this example, Spring will scan the com.example.project package and all its sub-packages, find all components, and register them in the application context automatically.