Annotation Interface ComponentScan
@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 (for example, 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
).
- Since:
- 3.1
- Author:
- Chris Beams, Juergen Hoeller, Sam Brannen
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic @interface
Declares the type filter to be used as an include filter or exclude filter. -
Optional Element Summary
Modifier and TypeOptional ElementDescriptionClass<?>[]
Type-safe alternative tobasePackages()
for specifying the packages to scan for annotated components.String[]
Base packages to scan for annotated components.Specifies which types are not eligible for component scanning.Specifies which types are eligible for component scanning.boolean
Specify whether scanned beans should be registered for lazy initialization.Class<? extends BeanNameGenerator>
TheBeanNameGenerator
class to be used for naming detected components within the Spring container.Controls the class files eligible for component detection.Indicates whether proxies should be generated for detected components, which may be necessary when using scopes in a proxy-style fashion.Class<? extends ScopeMetadataResolver>
TheScopeMetadataResolver
to be used for resolving the scope of detected components.boolean
Indicates whether automatic detection of classes annotated with@Component
@Repository
,@Service
, or@Controller
should be enabled.String[]
Alias forbasePackages()
.
-
Element Details
-
value
Alias forbasePackages()
.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:
- {}
-
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.- Default:
- {}
-
basePackageClasses
Class<?>[] basePackageClassesType-safe alternative tobasePackages()
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:
- {}
-
nameGenerator
Class<? extends BeanNameGenerator> nameGeneratorTheBeanNameGenerator
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, for example, the defaultAnnotationBeanNameGenerator
or any custom instance supplied to the application context at bootstrap time.- See Also:
- Default:
- org.springframework.beans.factory.support.BeanNameGenerator.class
-
scopeResolver
Class<? extends ScopeMetadataResolver> scopeResolverTheScopeMetadataResolver
to be used for resolving the scope of detected components.- Default:
- org.springframework.context.annotation.AnnotationScopeMetadataResolver.class
-
scopedProxy
ScopedProxyMode scopedProxyIndicates 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()
.- Default:
- DEFAULT
-
resourcePattern
String resourcePatternControls the class files eligible for component detection.Consider use of
includeFilters()
andexcludeFilters()
for a more flexible approach.- Default:
- "**/*.class"
-
useDefaultFilters
boolean useDefaultFiltersIndicates whether automatic detection of classes annotated with@Component
@Repository
,@Service
, or@Controller
should be enabled.- Default:
- true
-
includeFilters
ComponentScan.Filter[] includeFiltersSpecifies 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:
- Default:
- {}
-
excludeFilters
ComponentScan.Filter[] excludeFiltersSpecifies which types are not eligible for component scanning.- See Also:
- Default:
- {}
-
lazyInit
boolean lazyInitSpecify whether scanned beans should be registered for lazy initialization.Default is
false
; switch this totrue
when desired.- Since:
- 4.1
- Default:
- false
-