Top Description Fields Constructors Methods
org.springframework.context.annotation

public Class ClassPathScanningCandidateComponentProvider

extends Object
implements EnvironmentCapable, ResourceLoaderAware
Class Inheritance
All Implemented Interfaces
org.springframework.context.ResourceLoaderAware, org.springframework.beans.factory.Aware, org.springframework.core.env.EnvironmentCapable
Known Direct Subclasses
org.springframework.context.annotation.ClassPathBeanDefinitionScanner
Annotations
@SuppressWarnings:removal
Imports
java.io.FileNotFoundException, .IOException, java.lang.annotation.Annotation, java.util.ArrayList, .HashSet, .LinkedHashSet, .List, .Set, org.apache.commons.logging.Log, .LogFactory, org.springframework.beans.factory.BeanDefinitionStoreException, org.springframework.beans.factory.annotation.AnnotatedBeanDefinition, .Lookup, org.springframework.beans.factory.config.BeanDefinition, org.springframework.beans.factory.support.BeanDefinitionRegistry, org.springframework.context.ResourceLoaderAware, org.springframework.context.index.CandidateComponentsIndex, .CandidateComponentsIndexLoader, org.springframework.core.SpringProperties, org.springframework.core.annotation.AnnotationUtils, org.springframework.core.env.Environment, .EnvironmentCapable, .StandardEnvironment, org.springframework.core.io.Resource, .ResourceLoader, org.springframework.core.io.support.PathMatchingResourcePatternResolver, .ResourcePatternResolver, .ResourcePatternUtils, org.springframework.core.type.AnnotationMetadata, org.springframework.core.type.classreading.CachingMetadataReaderFactory, .ClassFormatException, .MetadataReader, .MetadataReaderFactory, org.springframework.core.type.filter.AnnotationTypeFilter, .AssignableTypeFilter, .TypeFilter, org.springframework.lang.Nullable, org.springframework.stereotype.Component, .Controller, .Indexed, .Repository, .Service, org.springframework.util.Assert, .ClassUtils

A component provider that scans for candidate components starting from a specified base package. Can use the component index, if it is available, and scans the classpath otherwise.

Candidate components are identified by applying exclude and include filters. AnnotationTypeFilter and AssignableTypeFilter include filters for an annotation/target-type that is annotated with Indexed are supported: if any other include filter is specified, the index is ignored and classpath scanning is used instead.

This implementation is based on Spring's MetadataReader facility, backed by an ASM ClassReader.

Authors
Mark Fisher, Juergen Hoeller, Ramnivas Laddad, Chris Beams, Stephane Nicoll, Sam Brannen
Since
2.5
See Also
org.springframework.core.type.classreading.MetadataReaderFactory, org.springframework.core.type.AnnotationMetadata, ScannedGenericBeanDefinition, CandidateComponentsIndex

Field Summary

Modifier and TypeField and Description
private CandidateComponentsIndex
componentsIndex

References Deprecated CandidateComponentsIndex is deprecated or references (maybe indirectly) at least one deprecated element.
private ConditionEvaluator
pack-priv static final String
private Environment
private final List<TypeFilter>
public static final String
IGNORE_CLASSFORMAT_PROPERTY_NAME

System property that instructs Spring to ignore class format exceptions during classpath scanning, in particular for unsupported class file versions.

private final List<TypeFilter>
protected final Log
private MetadataReaderFactory
private String
private ResourcePatternResolver
private static final boolean

Constructor Summary

AccessConstructor and Description
protected
ClassPathScanningCandidateComponentProvider()

Protected constructor for flexible subclass initialization.

public
ClassPathScanningCandidateComponentProvider(boolean
whether to register the default filters for the @Component, @Repository, @Service, and @Controller stereotype annotations
useDefaultFilters
)

Create a ClassPathScanningCandidateComponentProvider with a StandardEnvironment.

public
ClassPathScanningCandidateComponentProvider(boolean
whether to register the default filters for the @Component, @Repository, @Service, and @Controller stereotype annotations
useDefaultFilters
,
Environment
the Environment to use
environment
)

Create a ClassPathScanningCandidateComponentProvider with the given Environment.

Method Summary

Modifier and TypeMethod and Description
private Set<BeanDefinition>
addCandidateComponentsFromIndex(CandidateComponentsIndex index, String basePackage)
References Deprecated CandidateComponentsIndex is deprecated or references (maybe indirectly) at least one deprecated element.

public void
addExcludeFilter(TypeFilter excludeFilter)

Add an exclude type filter to the front of the exclusion list.

public void
addIncludeFilter(TypeFilter includeFilter)

Add an include type filter to the end of the inclusion list.

public void
clearCache()

Clear the local metadata cache, if any, removing all cached class metadata.

private String

Returns:

the stereotype in the index matching this filter
extractStereotype
(TypeFilter
the filter to handle
filter
)

Extract the stereotype to use for the specified compatible filter.

public Set<BeanDefinition>

Returns:

a corresponding Set of autodetected bean definitions
findCandidateComponents
(String
the package to check for annotated classes
basePackage
)

Scan the component index or class path for candidate components.

public final Environment
getEnvironment()

Implements org.springframework.core.env.EnvironmentCapable.getEnvironment.

Return the Environment associated with this component.

public final MetadataReaderFactory
getMetadataReaderFactory()

Return the MetadataReaderFactory used by this component provider.

protected BeanDefinitionRegistry
getRegistry()

Return the BeanDefinitionRegistry used by this scanner, if any.

public final ResourceLoader
getResourceLoader()

Return the ResourceLoader that this component provider uses.

private ResourcePatternResolver
private boolean

Returns:

whether the index supports this include filter
indexSupportsIncludeFilter
(TypeFilter
the filter to check
filter
)

Determine if the specified include TypeFilter is supported by the index.

private boolean

Returns:

true if the index is available and the configuration of this instance is supported by it, false otherwise
indexSupportsIncludeFilters
()

Determine if the component index can be used by this instance.

protected boolean

Returns:

whether the class qualifies as a candidate component
isCandidateComponent
(MetadataReader
the ASM ClassReader for the class
metadataReader
)

Determine whether the given class does not match any exclude filter and does match at least one include filter.

protected boolean

Returns:

whether the bean definition qualifies as a candidate component
isCandidateComponent
(AnnotatedBeanDefinition
the bean definition to check
beanDefinition
)

Determine whether the given bean definition qualifies as candidate.

private boolean

Returns:

whether the class qualifies as a candidate component
isConditionMatch
(MetadataReader
the ASM ClassReader for the class
metadataReader
)

Determine whether the given class is a candidate component based on any @Conditional annotations.

protected void
registerDefaultFilters()

Register the default filter for @Component.

public void
resetFilters(boolean
whether to re-register the default filters for the @Component, @Repository, @Service, and @Controller stereotype annotations
useDefaultFilters
)

Reset the configured type filters.

protected String

Returns:

the pattern specification to be used for package searching
resolveBasePackage
(String
the base package as specified by the user
basePackage
)

Resolve the specified base package into a pattern specification for the package search path.

private Set<BeanDefinition>
public void
setEnvironment(Environment
the Environment to use
environment
)

Set the Environment to use when resolving placeholders and evaluating @Conditional-annotated component classes.

public void
public void
setResourceLoader(ResourceLoader
the ResourceLoader object to be used by this object
resourceLoader
)

Implements org.springframework.context.ResourceLoaderAware.setResourceLoader.

Set the ResourceLoader to use for resource locations.

public void
setResourcePattern(String resourcePattern)

Set the resource pattern to use when scanning the classpath.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

componentsIndexback to summary
private CandidateComponentsIndex componentsIndex

References Deprecated

CandidateComponentsIndex is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Annotations
@Nullable
conditionEvaluatorback to summary
private ConditionEvaluator conditionEvaluator
Annotations
@Nullable
DEFAULT_RESOURCE_PATTERNback to summary
pack-priv static final String DEFAULT_RESOURCE_PATTERN
environmentback to summary
private Environment environment
Annotations
@Nullable
excludeFiltersback to summary
private final List<TypeFilter> excludeFilters
IGNORE_CLASSFORMAT_PROPERTY_NAMEback to summary
public static final String IGNORE_CLASSFORMAT_PROPERTY_NAME

System property that instructs Spring to ignore class format exceptions during classpath scanning, in particular for unsupported class file versions. By default, such a class format mismatch leads to a classpath scanning failure.

Since
6.1.2
See Also
ClassFormatException
includeFiltersback to summary
private final List<TypeFilter> includeFilters
loggerback to summary
protected final Log logger
metadataReaderFactoryback to summary
private MetadataReaderFactory metadataReaderFactory
Annotations
@Nullable
resourcePatternback to summary
private String resourcePattern
resourcePatternResolverback to summary
private ResourcePatternResolver resourcePatternResolver
Annotations
@Nullable
shouldIgnoreClassFormatExceptionback to summary
private static final boolean shouldIgnoreClassFormatException

Constructor Detail

ClassPathScanningCandidateComponentProviderback to summary
protected ClassPathScanningCandidateComponentProvider()

Protected constructor for flexible subclass initialization.

Since
4.3.6
ClassPathScanningCandidateComponentProviderback to summary
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)

Create a ClassPathScanningCandidateComponentProvider with a StandardEnvironment.

Parameters
useDefaultFilters:boolean

whether to register the default filters for the @Component, @Repository, @Service, and @Controller stereotype annotations

See Also
registerDefaultFilters()
ClassPathScanningCandidateComponentProviderback to summary
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment)

Create a ClassPathScanningCandidateComponentProvider with the given Environment.

Parameters
useDefaultFilters:boolean

whether to register the default filters for the @Component, @Repository, @Service, and @Controller stereotype annotations

environment:Environment

the Environment to use

See Also
registerDefaultFilters()

Method Detail

addCandidateComponentsFromIndexback to summary
private Set<BeanDefinition> addCandidateComponentsFromIndex(CandidateComponentsIndex index, String basePackage)

References Deprecated

CandidateComponentsIndex is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

addExcludeFilterback to summary
public void addExcludeFilter(TypeFilter excludeFilter)

Add an exclude type filter to the front of the exclusion list.

addIncludeFilterback to summary
public void addIncludeFilter(TypeFilter includeFilter)

Add an include type filter to the end of the inclusion list.

clearCacheback to summary
public void clearCache()

Clear the local metadata cache, if any, removing all cached class metadata.

extractStereotypeback to summary
private String extractStereotype(TypeFilter filter)

Extract the stereotype to use for the specified compatible filter.

Parameters
filter:TypeFilter

the filter to handle

Returns:String

the stereotype in the index matching this filter

Annotations
@Nullable
Since
5.0
See Also
indexSupportsIncludeFilter(TypeFilter)
findCandidateComponentsback to summary
public Set<BeanDefinition> findCandidateComponents(String basePackage)

Scan the component index or class path for candidate components.

Parameters
basePackage:String

the package to check for annotated classes

Returns:Set<BeanDefinition>

a corresponding Set of autodetected bean definitions

getEnvironmentback to summary
public final Environment getEnvironment()

Implements org.springframework.core.env.EnvironmentCapable.getEnvironment.

Doc from org.springframework.core.env.EnvironmentCapable.getEnvironment.

Return the Environment associated with this component.

Annotations
@Override
getMetadataReaderFactoryback to summary
public final MetadataReaderFactory getMetadataReaderFactory()

Return the MetadataReaderFactory used by this component provider.

getRegistryback to summary
protected BeanDefinitionRegistry getRegistry()

Return the BeanDefinitionRegistry used by this scanner, if any.

Annotations
@Nullable
getResourceLoaderback to summary
public final ResourceLoader getResourceLoader()

Return the ResourceLoader that this component provider uses.

getResourcePatternResolverback to summary
private ResourcePatternResolver getResourcePatternResolver()
indexSupportsIncludeFilterback to summary
private boolean indexSupportsIncludeFilter(TypeFilter filter)

Determine if the specified include TypeFilter is supported by the index.

Parameters
filter:TypeFilter

the filter to check

Returns:boolean

whether the index supports this include filter

Since
5.0
See Also
extractStereotype(TypeFilter)
indexSupportsIncludeFiltersback to summary
private boolean indexSupportsIncludeFilters()

Determine if the component index can be used by this instance.

Returns:boolean

true if the index is available and the configuration of this instance is supported by it, false otherwise

Since
5.0
isCandidateComponentback to summary
protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException

Determine whether the given class does not match any exclude filter and does match at least one include filter.

Parameters
metadataReader:MetadataReader

the ASM ClassReader for the class

Returns:boolean

whether the class qualifies as a candidate component

isCandidateComponentback to summary
protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition)

Determine whether the given bean definition qualifies as candidate.

The default implementation checks whether the class is not an interface and not dependent on an enclosing class.

Can be overridden in subclasses.

Parameters
beanDefinition:AnnotatedBeanDefinition

the bean definition to check

Returns:boolean

whether the bean definition qualifies as a candidate component

isConditionMatchback to summary
private boolean isConditionMatch(MetadataReader metadataReader)

Determine whether the given class is a candidate component based on any @Conditional annotations.

Parameters
metadataReader:MetadataReader

the ASM ClassReader for the class

Returns:boolean

whether the class qualifies as a candidate component

registerDefaultFiltersback to summary
protected void registerDefaultFilters()

Register the default filter for @Component.

This will implicitly register all annotations that have the @Component meta-annotation including the @Repository, @Service, and @Controller stereotype annotations.

Also supports Jakarta EE's jakarta.annotation.ManagedBean and JSR-330's jakarta.inject.Named annotations (as well as their pre-Jakarta javax.annotation.ManagedBean and javax.inject.Named equivalents), if available.

Annotations
@SuppressWarnings:unchecked
resetFiltersback to summary
public void resetFilters(boolean useDefaultFilters)

Reset the configured type filters.

Parameters
useDefaultFilters:boolean

whether to re-register the default filters for the @Component, @Repository, @Service, and @Controller stereotype annotations

See Also
registerDefaultFilters()
resolveBasePackageback to summary
protected String resolveBasePackage(String basePackage)

Resolve the specified base package into a pattern specification for the package search path.

The default implementation resolves placeholders against system properties, and converts a "."-based package path to a "/"-based resource path.

Parameters
basePackage:String

the base package as specified by the user

Returns:String

the pattern specification to be used for package searching

scanCandidateComponentsback to summary
private Set<BeanDefinition> scanCandidateComponents(String basePackage)
setEnvironmentback to summary
public void setEnvironment(Environment environment)

Set the Environment to use when resolving placeholders and evaluating @Conditional-annotated component classes.

The default is a StandardEnvironment.

Parameters
environment:Environment

the Environment to use

setMetadataReaderFactoryback to summary
public void setMetadataReaderFactory(MetadataReaderFactory metadataReaderFactory)

Set the MetadataReaderFactory to use.

Default is a CachingMetadataReaderFactory for the specified resource loader.

Call this setter method after setResourceLoader in order for the given MetadataReaderFactory to override the default factory.

setResourceLoaderback to summary
public void setResourceLoader(ResourceLoader resourceLoader)

Implements org.springframework.context.ResourceLoaderAware.setResourceLoader.

Set the ResourceLoader to use for resource locations. This will typically be a ResourcePatternResolver implementation.

Default is a PathMatchingResourcePatternResolver, also capable of resource pattern resolving through the ResourcePatternResolver interface.

Parameters
resourceLoader:ResourceLoader

Doc from org.springframework.context.ResourceLoaderAware.setResourceLoader.

the ResourceLoader object to be used by this object

Annotations
@Override
See Also
org.springframework.core.io.support.ResourcePatternResolver, org.springframework.core.io.support.PathMatchingResourcePatternResolver
setResourcePatternback to summary
public void setResourcePattern(String resourcePattern)

Set the resource pattern to use when scanning the classpath. This value will be appended to each base package name.

See Also
findCandidateComponents(String), DEFAULT_RESOURCE_PATTERN