Senior
What is @ComponentScan used for?
sobes.tech AI
Answer from AI
The @ComponentScan annotation in Spring is used for automatic scanning of packages for components such as classes annotated with @Component, @Service, @Repository, and others that need to be registered in the Spring context.
This allows you to avoid manually defining each bean and instead automatically discover and instantiate them.
Example of usage:
@Configuration
@ComponentScan(basePackages = "com.example.project")
public class AppConfig {
// Spring configuration
}
Here, Spring will scan the com.example.project package and all its sub-packages, find all components, and register them in the context.
Thus, @ComponentScan simplifies application configuration and promotes modularity and ease of maintenance.