Sobes.tech
Junior — Middle

What is the role of the @ComponentScan annotation in the context of automatic component registration in Spring?

sobes.tech AI

Answer from AI

The @ComponentScan annotation in Spring indicates to the framework which packages to scan for classes annotated with @Component, @Service, @Repository, etc., in order to automatically register them as beans in the application context.

Without @ComponentScan, Spring will not scan the specified packages, and components will not be created automatically.

Example of usage:

@Configuration
@ComponentScan(basePackages = "com.example.app.services")
public class AppConfig {
    // Application configuration
}

Here, Spring will scan the package com.example.app.services and register all components found there.