Annotation Type ComponentScan


  • @Retention(RUNTIME)
    @Target(TYPE)
    @Documented
    @Repeatable(ComponentScans.class)
    public @interface ComponentScan
    Configures component scanning directives for use with @Configuration classes. Provides support parallel with Spring XML's <context:component-scan> element.

    Either basePackageClasses() or basePackages() (or its alias value()) may be specified to define specific packages to scan. If specific packages are not defined, scanning will occur from the package of the class that declares this annotation.

    Note that the <context:component-scan> element has an annotation-config attribute; however, this annotation does not. This is because in almost all cases when using @ComponentScan, default annotation config processing (e.g. processing @Autowired and friends) is assumed. Furthermore, when using AnnotationConfigApplicationContext, annotation config processors are always registered, meaning that any attempt to disable them at the @ComponentScan level would be ignored.

    See @Configuration's Javadoc for usage examples.

    Since:
    3.1
    Author:
    Chris Beams, Juergen Hoeller, Sam Brannen
    See Also:
    Configuration
    • Element Detail

      • value

        @AliasFor("basePackages")
        String[] value
        Alias for basePackages().

        Allows for more concise annotation declarations if no other attributes are needed — for example, @ComponentScan("org.my.pkg") instead of @ComponentScan(basePackages = "org.my.pkg").

        Default:
        {}
      • basePackageClasses

        Class<?>[] basePackageClasses
        Type-safe alternative to basePackages() for specifying the packages to scan for annotated components. The package of each class specified will be scanned.

        Consider creating a special no-op marker class or interface in each package that serves no purpose other than being referenced by this attribute.

        Default:
        {}
      • useDefaultFilters

        boolean useDefaultFilters
        Indicates whether automatic detection of classes annotated with @Component@Repository, @Service, or @Controller should be enabled.
        Default:
        true
      • includeFilters

        ComponentScan.Filter[] includeFilters
        Specifies which types are eligible for component scanning.

        Further narrows the set of candidate components from everything in basePackages() to everything in the base packages that matches the given filter or filters.

        Note that these filters will be applied in addition to the default filters, if specified. Any type under the specified base packages which matches a given filter will be included, even if it does not match the default filters (i.e. is not annotated with @Component).

        See Also:
        resourcePattern(), useDefaultFilters()
        Default:
        {}
      • lazyInit

        boolean lazyInit
        Specify whether scanned beans should be registered for lazy initialization.

        Default is false; switch this to true when desired.

        Since:
        4.1
        Default:
        false