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

public Class ClassPathBeanDefinitionScanner

extends ClassPathScanningCandidateComponentProvider
Class Inheritance
Imports
java.util.LinkedHashSet, .Set, org.springframework.beans.factory.annotation.AnnotatedBeanDefinition, org.springframework.beans.factory.config.BeanDefinition, .BeanDefinitionHolder, org.springframework.beans.factory.support.AbstractBeanDefinition, .BeanDefinitionDefaults, .BeanDefinitionReaderUtils, .BeanDefinitionRegistry, .BeanNameGenerator, org.springframework.core.env.Environment, .EnvironmentCapable, .StandardEnvironment, org.springframework.core.io.ResourceLoader, org.springframework.lang.Nullable, org.springframework.util.Assert, .ObjectUtils, .PatternMatchUtils

A bean definition scanner that detects bean candidates on the classpath, registering corresponding bean definitions with a given registry (BeanFactory or ApplicationContext).

Candidate classes are detected through configurable type filters. The default filters include classes that are annotated with Spring's @Component, @Repository, @Service, or @Controller stereotype.

Also supports Jakarta EE's jakarta.annotation.ManagedBean and JSR-330's jakarta.inject.Named annotations, if available.

Authors
Mark Fisher, Juergen Hoeller, Chris Beams
Since
2.5
See Also
AnnotationConfigApplicationContext#scan, org.springframework.stereotype.Component, org.springframework.stereotype.Repository, org.springframework.stereotype.Service, org.springframework.stereotype.Controller

Field Summary

Modifier and TypeField and Description
private String[]
private BeanDefinitionDefaults
private BeanNameGenerator
private boolean
private final BeanDefinitionRegistry
private ScopeMetadataResolver
Inherited from org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider:
DEFAULT_RESOURCE_PATTERNIGNORE_CLASSFORMAT_PROPERTY_NAMElogger

Constructor Summary

AccessConstructor and Description
public
ClassPathBeanDefinitionScanner(BeanDefinitionRegistry
the BeanFactory to load bean definitions into, in the form of a BeanDefinitionRegistry
registry
)

Create a new ClassPathBeanDefinitionScanner for the given bean factory.

public
ClassPathBeanDefinitionScanner(BeanDefinitionRegistry
the BeanFactory to load bean definitions into, in the form of a BeanDefinitionRegistry
registry
,
boolean
whether to include the default filters for the @Component, @Repository, @Service, and @Controller stereotype annotations
useDefaultFilters
)

Create a new ClassPathBeanDefinitionScanner for the given bean factory.

public
ClassPathBeanDefinitionScanner(BeanDefinitionRegistry
the BeanFactory to load bean definitions into, in the form of a BeanDefinitionRegistry
registry
,
boolean
whether to include the default filters for the @Component, @Repository, @Service, and @Controller stereotype annotations
useDefaultFilters
,
Environment
the Spring Environment to use when evaluating bean definition profile metadata
environment
)

Create a new ClassPathBeanDefinitionScanner for the given bean factory and using the given Environment when evaluating bean definition profile metadata.

public
ClassPathBeanDefinitionScanner(BeanDefinitionRegistry
the BeanFactory to load bean definitions into, in the form of a BeanDefinitionRegistry
registry
,
boolean
whether to include the default filters for the @Component, @Repository, @Service, and @Controller stereotype annotations
useDefaultFilters
,
Environment
the Spring Environment to use when evaluating bean definition profile metadata
environment
,
ResourceLoader
the ResourceLoader to use
resourceLoader
)

Create a new ClassPathBeanDefinitionScanner for the given bean factory and using the given Environment when evaluating bean definition profile metadata.

Method Summary

Modifier and TypeMethod and Description
protected boolean

Returns:

true if the bean can be registered as-is; false if it should be skipped because there is an existing, compatible bean definition for the specified name
checkCandidate
(String
the suggested name for the bean
beanName
,
BeanDefinition
the corresponding bean definition
beanDefinition
)

Check the given candidate's bean name, determining whether the corresponding bean definition needs to be registered or conflicts with an existing definition.

protected Set<BeanDefinitionHolder>

Returns:

set of beans registered if any for tooling registration purposes (never null)
doScan
(String...
the packages to check for annotated classes
basePackages
)

Perform a scan within the specified base packages, returning the registered bean definitions.

public BeanDefinitionDefaults
getBeanDefinitionDefaults()

Return the defaults to use for detected beans (never null).

private static Environment
getOrCreateEnvironment(BeanDefinitionRegistry registry)

Get the Environment from the given registry if possible, otherwise return a new StandardEnvironment.

public final BeanDefinitionRegistry
getRegistry()

Overrides org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.getRegistry.

Return the BeanDefinitionRegistry that this scanner operates on.

protected boolean

Returns:

whether the definitions are considered as compatible, with the new definition to be skipped in favor of the existing definition
isCompatible
(BeanDefinition
the new bean definition, originated from scanning
newDef
,
BeanDefinition
the existing bean definition, potentially an explicitly defined one or a previously generated one from scanning
existingDef
)

Determine whether the given new bean definition is compatible with the given existing bean definition.

protected void
postProcessBeanDefinition(AbstractBeanDefinition
the scanned bean definition
beanDefinition
,
String
the generated bean name for the given bean
beanName
)

Apply further settings to the given bean definition, beyond the contents retrieved from scanning the component class.

protected void
registerBeanDefinition(BeanDefinitionHolder
the bean definition plus bean name for the bean
definitionHolder
,
BeanDefinitionRegistry
the BeanDefinitionRegistry to register the bean with
registry
)

Register the specified bean with the given registry.

public int

Returns:

number of beans registered
scan
(String...
the packages to check for annotated classes
basePackages
)

Perform a scan within the specified base packages.

public void
setAutowireCandidatePatterns(String...
the patterns to match against
autowireCandidatePatterns
)

Set the name-matching patterns for determining autowire candidates.

public void
setBeanDefinitionDefaults(BeanDefinitionDefaults beanDefinitionDefaults)

Set the defaults to use for detected beans.

public void
setBeanNameGenerator(BeanNameGenerator beanNameGenerator)

Set the BeanNameGenerator to use for detected bean classes.

public void
setIncludeAnnotationConfig(boolean includeAnnotationConfig)

Specify whether to register annotation config post-processors.

public void
setScopedProxyMode(ScopedProxyMode scopedProxyMode)

Specify the proxy behavior for non-singleton scoped beans.

public void
setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)

Set the ScopeMetadataResolver to use for detected bean classes.

Inherited from org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider:
addExcludeFilteraddIncludeFilterclearCachefindCandidateComponentsgetEnvironmentgetMetadataReaderFactorygetResourceLoaderisCandidateComponentisCandidateComponentregisterDefaultFiltersresetFiltersresolveBasePackagesetEnvironmentsetMetadataReaderFactorysetResourceLoadersetResourcePattern

Field Detail

autowireCandidatePatternsback to summary
private String[] autowireCandidatePatterns
Annotations
@Nullable
beanDefinitionDefaultsback to summary
private BeanDefinitionDefaults beanDefinitionDefaults
beanNameGeneratorback to summary
private BeanNameGenerator beanNameGenerator
includeAnnotationConfigback to summary
private boolean includeAnnotationConfig
registryback to summary
private final BeanDefinitionRegistry registry
scopeMetadataResolverback to summary
private ScopeMetadataResolver scopeMetadataResolver

Constructor Detail

ClassPathBeanDefinitionScannerback to summary
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry)

Create a new ClassPathBeanDefinitionScanner for the given bean factory.

Parameters
registry:BeanDefinitionRegistry

the BeanFactory to load bean definitions into, in the form of a BeanDefinitionRegistry

ClassPathBeanDefinitionScannerback to summary
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters)

Create a new ClassPathBeanDefinitionScanner for the given bean factory.

If the passed-in bean factory does not only implement the BeanDefinitionRegistry interface but also the ResourceLoader interface, it will be used as default ResourceLoader as well. This will usually be the case for org.springframework.context.ApplicationContext implementations.

If given a plain BeanDefinitionRegistry, the default ResourceLoader will be a org.springframework.core.io.support.PathMatchingResourcePatternResolver.

If the passed-in bean factory also implements EnvironmentCapable its environment will be used by this reader. Otherwise, the reader will initialize and use a org.springframework.core.env.StandardEnvironment. All ApplicationContext implementations are EnvironmentCapable, while normal BeanFactory implementations are not.

Parameters
registry:BeanDefinitionRegistry

the BeanFactory to load bean definitions into, in the form of a BeanDefinitionRegistry

useDefaultFilters:boolean

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

See Also
setResourceLoader, setEnvironment
ClassPathBeanDefinitionScannerback to summary
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment)

Create a new ClassPathBeanDefinitionScanner for the given bean factory and using the given Environment when evaluating bean definition profile metadata.

If the passed-in bean factory does not only implement the BeanDefinitionRegistry interface but also the ResourceLoader interface, it will be used as default ResourceLoader as well. This will usually be the case for org.springframework.context.ApplicationContext implementations.

If given a plain BeanDefinitionRegistry, the default ResourceLoader will be a org.springframework.core.io.support.PathMatchingResourcePatternResolver.

Parameters
registry:BeanDefinitionRegistry

the BeanFactory to load bean definitions into, in the form of a BeanDefinitionRegistry

useDefaultFilters:boolean

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

environment:Environment

the Spring Environment to use when evaluating bean definition profile metadata

Since
3.1
See Also
setResourceLoader
ClassPathBeanDefinitionScannerback to summary
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment, ResourceLoader resourceLoader)

Create a new ClassPathBeanDefinitionScanner for the given bean factory and using the given Environment when evaluating bean definition profile metadata.

Parameters
registry:BeanDefinitionRegistry

the BeanFactory to load bean definitions into, in the form of a BeanDefinitionRegistry

useDefaultFilters:boolean

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

environment:Environment

the Spring Environment to use when evaluating bean definition profile metadata

resourceLoader:ResourceLoader

the ResourceLoader to use

Since
4.3.6

Method Detail

checkCandidateback to summary
protected boolean checkCandidate(String beanName, BeanDefinition beanDefinition) throws IllegalStateException

Check the given candidate's bean name, determining whether the corresponding bean definition needs to be registered or conflicts with an existing definition.

Parameters
beanName:String

the suggested name for the bean

beanDefinition:BeanDefinition

the corresponding bean definition

Returns:boolean

true if the bean can be registered as-is; false if it should be skipped because there is an existing, compatible bean definition for the specified name

Exceptions
IllegalStateException:
if an existing, incompatible bean definition has been found for the specified name
doScanback to summary
protected Set<BeanDefinitionHolder> doScan(String... basePackages)

Perform a scan within the specified base packages, returning the registered bean definitions.

This method does not register an annotation config processor but rather leaves this up to the caller.

Parameters
basePackages:String[]

the packages to check for annotated classes

Returns:Set<BeanDefinitionHolder>

set of beans registered if any for tooling registration purposes (never null)

getBeanDefinitionDefaultsback to summary
public BeanDefinitionDefaults getBeanDefinitionDefaults()

Return the defaults to use for detected beans (never null).

Since
4.1
getOrCreateEnvironmentback to summary
private static Environment getOrCreateEnvironment(BeanDefinitionRegistry registry)

Get the Environment from the given registry if possible, otherwise return a new StandardEnvironment.

getRegistryback to summary
public final BeanDefinitionRegistry getRegistry()

Overrides org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.getRegistry.

Return the BeanDefinitionRegistry that this scanner operates on.

Annotations
@Override
isCompatibleback to summary
protected boolean isCompatible(BeanDefinition newDef, BeanDefinition existingDef)

Determine whether the given new bean definition is compatible with the given existing bean definition.

The default implementation considers them as compatible when the existing bean definition comes from the same source or from a non-scanning source.

Parameters
newDef:BeanDefinition

the new bean definition, originated from scanning

existingDef:BeanDefinition

the existing bean definition, potentially an explicitly defined one or a previously generated one from scanning

Returns:boolean

whether the definitions are considered as compatible, with the new definition to be skipped in favor of the existing definition

postProcessBeanDefinitionback to summary
protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName)

Apply further settings to the given bean definition, beyond the contents retrieved from scanning the component class.

Parameters
beanDefinition:AbstractBeanDefinition

the scanned bean definition

beanName:String

the generated bean name for the given bean

registerBeanDefinitionback to summary
protected void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)

Register the specified bean with the given registry.

Can be overridden in subclasses, e.g. to adapt the registration process or to register further bean definitions for each scanned bean.

Parameters
definitionHolder:BeanDefinitionHolder

the bean definition plus bean name for the bean

registry:BeanDefinitionRegistry

the BeanDefinitionRegistry to register the bean with

scanback to summary
public int scan(String... basePackages)

Perform a scan within the specified base packages.

Parameters
basePackages:String[]

the packages to check for annotated classes

Returns:int

number of beans registered

setAutowireCandidatePatternsback to summary
public void setAutowireCandidatePatterns(String... autowireCandidatePatterns)

Set the name-matching patterns for determining autowire candidates.

Parameters
autowireCandidatePatterns:String[]

the patterns to match against

setBeanDefinitionDefaultsback to summary
public void setBeanDefinitionDefaults(BeanDefinitionDefaults beanDefinitionDefaults)

Set the defaults to use for detected beans.

See Also
BeanDefinitionDefaults
setBeanNameGeneratorback to summary
public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator)

Set the BeanNameGenerator to use for detected bean classes.

Default is a AnnotationBeanNameGenerator.

setIncludeAnnotationConfigback to summary
public void setIncludeAnnotationConfig(boolean includeAnnotationConfig)

Specify whether to register annotation config post-processors.

The default is to register the post-processors. Turn this off to be able to ignore the annotations or to process them differently.

setScopedProxyModeback to summary
public void setScopedProxyMode(ScopedProxyMode scopedProxyMode)

Specify the proxy behavior for non-singleton scoped beans. Note that this will override any custom "scopeMetadataResolver" setting.

The default is ScopedProxyMode#NO.

See Also
setScopeMetadataResolver
setScopeMetadataResolverback to summary
public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)

Set the ScopeMetadataResolver to use for detected bean classes. Note that this will override any custom "scopedProxyMode" setting.

The default is an AnnotationScopeMetadataResolver.

See Also
setScopedProxyMode