Top Description Inners Methods
org.springframework.context.annotation

public @Interface ComponentScan

extends Annotation
Annotations
@Retention:RUNTIME
@Target:TYPE
@Documented
@Repeatable:ComponentScans
Imports
java.lang.annotation.Documented, .ElementType, .Repeatable, .Retention, .RetentionPolicy, .Target, org.springframework.beans.factory.support.BeanNameGenerator, org.springframework.core.annotation.AliasFor, org.springframework.core.type.filter.TypeFilter

Configures component scanning directives for use with @Configuration classes.

Provides support comparable to Spring's <context:component-scan> XML namespace 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 recursively beginning with 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.

@ComponentScan can be used as a repeatable annotation. @ComponentScan may also be used as a meta-annotation to create custom composed annotations with attribute overrides.

Locally declared @ComponentScan annotations always take precedence over and effectively hide @ComponentScan meta-annotations, which allows explicit local configuration to override configuration that is meta-present (including composed annotations meta-annotated with @ComponentScan).

Authors
Chris Beams, Juergen Hoeller, Sam Brannen
Since
3.1
See Also
Configuration

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static @interface
ComponentScan.Filter

Declares the type filter to be used as an include filter or exclude filter.

Method Summary

Modifier and TypeMethod and Description
public Class<?>[]
basePackageClasses()

Type-safe alternative to basePackages for specifying the packages to scan for annotated components.

public String[]
basePackages()

Base packages to scan for annotated components.

public ComponentScan.Filter[]
excludeFilters()

Specifies which types are not eligible for component scanning.

public ComponentScan.Filter[]
includeFilters()

Specifies which types are eligible for component scanning.

public boolean
lazyInit()

Specify whether scanned beans should be registered for lazy initialization.

public Class<? extends BeanNameGenerator>
nameGenerator()

The BeanNameGenerator class to be used for naming detected components within the Spring container.

public String
resourcePattern()

Controls the class files eligible for component detection.

public ScopedProxyMode
scopedProxy()

Indicates whether proxies should be generated for detected components, which may be necessary when using scopes in a proxy-style fashion.

public Class<? extends ScopeMetadataResolver>
scopeResolver()

The ScopeMetadataResolver to be used for resolving the scope of detected components.

public boolean
useDefaultFilters()

Indicates whether automatic detection of classes annotated with @Component @Repository, @Service, or @Controller should be enabled.

public String[]
value()

Alias for basePackages.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

basePackageClassesback to summary
public 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.

basePackagesback to summary
public String[] basePackages()

Base packages to scan for annotated components.

value is an alias for (and mutually exclusive with) this attribute.

Use basePackageClasses for a type-safe alternative to String-based package names.

Annotations
@AliasFor:value
excludeFiltersback to summary
public ComponentScan.Filter[] excludeFilters()

Specifies which types are not eligible for component scanning.

See Also
resourcePattern
includeFiltersback to summary
public 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()
lazyInitback to summary
public boolean lazyInit()

Specify whether scanned beans should be registered for lazy initialization.

Default is false; switch this to true when desired.

Since
4.1
nameGeneratorback to summary
public Class<? extends BeanNameGenerator> nameGenerator()

The BeanNameGenerator class to be used for naming detected components within the Spring container.

The default value of the BeanNameGenerator interface itself indicates that the scanner used to process this @ComponentScan annotation should use its inherited bean name generator, e.g. the default AnnotationBeanNameGenerator or any custom instance supplied to the application context at bootstrap time.

See Also
AnnotationConfigApplicationContext#setBeanNameGenerator(BeanNameGenerator), AnnotationBeanNameGenerator, FullyQualifiedAnnotationBeanNameGenerator
resourcePatternback to summary
public String resourcePattern()

Controls the class files eligible for component detection.

Consider use of includeFilters and excludeFilters for a more flexible approach.

scopedProxyback to summary
public ScopedProxyMode scopedProxy()

Indicates whether proxies should be generated for detected components, which may be necessary when using scopes in a proxy-style fashion.

The default is defer to the default behavior of the component scanner used to execute the actual scan.

Note that setting this attribute overrides any value set for scopeResolver.

See Also
ClassPathBeanDefinitionScanner#setScopedProxyMode(ScopedProxyMode)
scopeResolverback to summary
public Class<? extends ScopeMetadataResolver> scopeResolver()

The ScopeMetadataResolver to be used for resolving the scope of detected components.

useDefaultFiltersback to summary
public boolean useDefaultFilters()

Indicates whether automatic detection of classes annotated with @Component @Repository, @Service, or @Controller should be enabled.

valueback to summary
public 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").

Annotations
@AliasFor:basePackages
org.springframework.context.annotation back to summary

public @Interface ComponentScan.Filter

extends Annotation
Annotations
@Retention:RUNTIME
@Target

Declares the type filter to be used as an include filter or exclude filter.

Method Summary

Modifier and TypeMethod and Description
public Class<?>[]
classes()

The class or classes to use as the filter.

public String[]
pattern()

The pattern (or patterns) to use for the filter, as an alternative to specifying a Class value.

public FilterType
type()

The type of filter to use.

public Class<?>[]
value()

Alias for classes.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

classesback to summary
public Class<?>[] classes()

The class or classes to use as the filter.

The following table explains how the classes will be interpreted based on the configured value of the type attribute.

FilterTypeClass Interpreted As
ANNOTATION the annotation itself
ASSIGNABLE_TYPE the type that detected components should be assignable to
CUSTOM an implementation of TypeFilter

When multiple classes are specified, OR logic is applied — for example, "include types annotated with @Foo OR @Bar".

Custom TypeFilters may optionally implement any of the following Aware interfaces, and their respective methods will be called prior to match:

Specifying zero classes is permitted but will have no effect on component scanning.

Annotations
@AliasFor:value
Since
4.2
See Also
value, type
patternback to summary
public String[] pattern()

The pattern (or patterns) to use for the filter, as an alternative to specifying a Class value.

If type is set to ASPECTJ, this is an AspectJ type pattern expression. If type is set to REGEX, this is a regex pattern for the fully-qualified class names to match.

See Also
type, classes
typeback to summary
public FilterType type()

The type of filter to use.

Default is FilterType#ANNOTATION.

See Also
classes, pattern
valueback to summary
public Class<?>[] value()

Alias for classes.

Annotations
@AliasFor:classes
See Also
classes