🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

Spring @ComponentScan Annotation


Introduction

  • @ComponentScan annotation was introduced in Spring 2.5, which is used to support "Annotation-based Configuration".

  • It enables Spring’s "Component-Scanning Feature", which allows Spring to automatically look for components, configurations and services to manage as Spring beans.
  • It scans one or more base packages and their sub-packages for classes annotated with component-related stereotypes (like @Component, @Service, @Repository, and @Controller) and registers these classes as beans within the Spring Context.

  • Syntax :
    Below is the syntax of @ComponentScan which is placed above a class definition:
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan(basePackages = "in.sp.package")
    public class AppConfig
    {
        // Application configuration
    }
  • NOTE : @ComponentScan annotation is commonly used in conjunction with @Configuration to register components or it’s embedded within @SpringBootApplication in Spring Boot.

  • Click Here for Spring Program using Annotation-Based Configurations in which @ComponentScan is used.

Common Usages of @ComponentScan Annotation

1. Standard Scanning for a Base Package
  • The most common use case is scanning a single base package where most of our components exists. This registers all @Component-annotated classes (including @Service, @Repository, and @Controller).
  • Syntax :
    @Configuration
    @ComponentScan(basePackages = "in.sp.package")
    public class AppConfig
    {
        // Scans in.sp.package and its sub-packages
    }
2. Multiple Base Packages
  • If we have components spread across different packages, then we can use "basePackages" to specify multiple packages.
  • Syntax :
    @Configuration
    @ComponentScan(basePackages = {"in.sp.service", "in.sp.repository"})
    public class AppConfig
    {
        // Scans two different packages
    }
3. Scanning Using basePackageClasses
  • Instead of hardcoding package names, we can use basePackageClasses which defines a class whose package Spring will scan.
  • Syntax :
    @Configuration
    @ComponentScan(basePackageClasses = {ServiceClass.class, RepositoryClass.class})
    public class AppConfig
    {
        // Scans packages of ServiceClass and RepositoryClass
    }
    
4. Custom Filtering with includeFilters and excludeFilters
  • Filters allow selective inclusion or exclusion of components during scanning.
  • includeFilters specifies types or annotations to include, while excludeFilters specifies those to exclude.
  • Syntax :
    @Configuration
    @ComponentScan(
        basePackages = "in.sp.package",
        includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyAnnotation.class),
        excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = UnwantedClass.class)
    )
    public class AppConfig
    {
        // Includes classes annotated with MyAnnotation but excludes UnwantedClass
    }
5. Lazy Initialization of Beans
  • We can set lazyInit to true if we want Spring to delay bean instantiation until the bean is needed, which can improve startup time for large applications.
  • Syntax :
    @Configuration
    @ComponentScan(basePackages = "in.sp.package", lazyInit = true)
    public class AppConfig
    {
        // Scans in.sp.package but initializes beans lazily
    }