Class ClassPathBeanDefinitionScanner
- All Implemented Interfaces:
- Aware,- ResourceLoaderAware,- EnvironmentCapable
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 JSR-330's Named annotations, if available.
- Since:
- 2.5
- Author:
- Mark Fisher, Juergen Hoeller, Chris Beams
- See Also:
- 
Field SummaryFields inherited from class org.springframework.context.annotation.ClassPathScanningCandidateComponentProviderIGNORE_CLASSFORMAT_PROPERTY_NAME, logger
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a newClassPathBeanDefinitionScannerfor the given bean factory.ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) Create a newClassPathBeanDefinitionScannerfor the given bean factory.ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment) Create a newClassPathBeanDefinitionScannerfor the given bean factory and using the givenEnvironmentwhen evaluating bean definition profile metadata.ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment, @Nullable ResourceLoader resourceLoader) Create a newClassPathBeanDefinitionScannerfor the given bean factory and using the givenEnvironmentwhen evaluating bean definition profile metadata.
- 
Method SummaryModifier and TypeMethodDescriptionprotected booleancheckCandidate(String beanName, BeanDefinition 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> Perform a scan within the specified base packages, returning the registered bean definitions.Return the defaults to use for detected beans (nevernull).final BeanDefinitionRegistryReturn the BeanDefinitionRegistry that this scanner operates on.protected booleanisCompatible(BeanDefinition newDef, BeanDefinition existingDef) Determine whether the given new bean definition is compatible with the given existing bean definition.protected voidpostProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName) Apply further settings to the given bean definition, beyond the contents retrieved from scanning the component class.protected voidregisterBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) Register the specified bean with the given registry.intPerform a scan within the specified base packages.voidsetAutowireCandidatePatterns(String @Nullable ... autowireCandidatePatterns) Set the name-matching patterns for determining autowire candidates.voidsetBeanDefinitionDefaults(@Nullable BeanDefinitionDefaults beanDefinitionDefaults) Set the defaults to use for detected beans.voidsetBeanNameGenerator(@Nullable BeanNameGenerator beanNameGenerator) Set the BeanNameGenerator to use for detected bean classes.voidsetIncludeAnnotationConfig(boolean includeAnnotationConfig) Specify whether to register annotation config post-processors.voidsetScopedProxyMode(ScopedProxyMode scopedProxyMode) Specify the proxy behavior for non-singleton scoped beans.voidsetScopeMetadataResolver(@Nullable ScopeMetadataResolver scopeMetadataResolver) Set the ScopeMetadataResolver to use for detected bean classes.Methods inherited from class org.springframework.context.annotation.ClassPathScanningCandidateComponentProvideraddExcludeFilter, addIncludeFilter, clearCache, findCandidateComponents, getEnvironment, getMetadataReaderFactory, getResourceLoader, isCandidateComponent, isCandidateComponent, registerDefaultFilters, resetFilters, resolveBasePackage, setEnvironment, setMetadataReaderFactory, setResourceLoader, setResourcePattern
- 
Constructor Details- 
ClassPathBeanDefinitionScannerCreate a newClassPathBeanDefinitionScannerfor the given bean factory.- Parameters:
- registry- the- BeanFactoryto load bean definitions into, in the form of a- BeanDefinitionRegistry
 
- 
ClassPathBeanDefinitionScannerCreate a newClassPathBeanDefinitionScannerfor the given bean factory.If the passed-in bean factory does not only implement the BeanDefinitionRegistryinterface but also theResourceLoaderinterface, it will be used as defaultResourceLoaderas well. This will usually be the case forApplicationContextimplementations.If given a plain BeanDefinitionRegistry, the defaultResourceLoaderwill be aPathMatchingResourcePatternResolver.If the passed-in bean factory also implements EnvironmentCapableits environment will be used by this reader. Otherwise, the reader will initialize and use aStandardEnvironment. AllApplicationContextimplementations areEnvironmentCapable, while normalBeanFactoryimplementations are not.- Parameters:
- registry- the- BeanFactoryto load bean definitions into, in the form of a- BeanDefinitionRegistry
- useDefaultFilters- whether to include the default filters for the- @Component,- @Repository,- @Service, and- @Controllerstereotype annotations
- See Also:
 
- 
ClassPathBeanDefinitionScannerpublic ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment) Create a newClassPathBeanDefinitionScannerfor the given bean factory and using the givenEnvironmentwhen evaluating bean definition profile metadata.If the passed-in bean factory does not only implement the BeanDefinitionRegistryinterface but also theResourceLoaderinterface, it will be used as defaultResourceLoaderas well. This will usually be the case forApplicationContextimplementations.If given a plain BeanDefinitionRegistry, the defaultResourceLoaderwill be aPathMatchingResourcePatternResolver.- Parameters:
- registry- the- BeanFactoryto load bean definitions into, in the form of a- BeanDefinitionRegistry
- useDefaultFilters- whether to include the default filters for the- @Component,- @Repository,- @Service, and- @Controllerstereotype annotations
- environment- the Spring- Environmentto use when evaluating bean definition profile metadata
- Since:
- 3.1
- See Also:
 
- 
ClassPathBeanDefinitionScannerpublic ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment, @Nullable ResourceLoader resourceLoader) Create a newClassPathBeanDefinitionScannerfor the given bean factory and using the givenEnvironmentwhen evaluating bean definition profile metadata.- Parameters:
- registry- the- BeanFactoryto load bean definitions into, in the form of a- BeanDefinitionRegistry
- useDefaultFilters- whether to include the default filters for the- @Component,- @Repository,- @Service, and- @Controllerstereotype annotations
- environment- the Spring- Environmentto use when evaluating bean definition profile metadata
- resourceLoader- the- ResourceLoaderto use
- Since:
- 4.3.6
 
 
- 
- 
Method Details- 
getRegistryReturn the BeanDefinitionRegistry that this scanner operates on.- Overrides:
- getRegistryin class- ClassPathScanningCandidateComponentProvider
 
- 
setBeanDefinitionDefaultsSet the defaults to use for detected beans.- See Also:
 
- 
getBeanDefinitionDefaultsReturn the defaults to use for detected beans (nevernull).- Since:
- 4.1
 
- 
setAutowireCandidatePatterns
- 
setBeanNameGeneratorSet the BeanNameGenerator to use for detected bean classes.Default is a AnnotationBeanNameGenerator.
- 
setScopeMetadataResolverSet the ScopeMetadataResolver to use for detected bean classes. Note that this will override any custom "scopedProxyMode" setting.The default is an AnnotationScopeMetadataResolver.- See Also:
 
- 
setScopedProxyModeSpecify the proxy behavior for non-singleton scoped beans. Note that this will override any custom "scopeMetadataResolver" setting.The default is ScopedProxyMode.NO.- See Also:
 
- 
setIncludeAnnotationConfigpublic 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. 
- 
scanPerform a scan within the specified base packages.- Parameters:
- basePackages- the packages to check for annotated classes
- Returns:
- number of beans registered
 
- 
doScanPerform 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- the packages to check for annotated classes
- Returns:
- set of beans registered if any for tooling registration purposes (never null)
 
- 
postProcessBeanDefinitionApply further settings to the given bean definition, beyond the contents retrieved from scanning the component class.- Parameters:
- beanDefinition- the scanned bean definition
- beanName- the generated bean name for the given bean
 
- 
registerBeanDefinitionprotected void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry) Register the specified bean with the given registry.Can be overridden in subclasses, for example, to adapt the registration process or to register further bean definitions for each scanned bean. - Parameters:
- definitionHolder- the bean definition plus bean name for the bean
- registry- the BeanDefinitionRegistry to register the bean with
 
- 
checkCandidateprotected 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- the suggested name for the bean
- beanDefinition- the corresponding bean definition
- Returns:
- trueif the bean can be registered as-is;- falseif it should be skipped because there is an existing, compatible bean definition for the specified name
- Throws:
- IllegalStateException- if an existing, incompatible bean definition has been found for the specified name
 
- 
isCompatibleDetermine 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- the new bean definition, originated from scanning
- existingDef- the existing bean definition, potentially an explicitly defined one or a previously generated one from scanning
- Returns:
- whether the definitions are considered as compatible, with the new definition to be skipped in favor of the existing definition
 
 
-