类 ClassPathBeanDefinitionScanner
- java.lang.Object
- org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider
- org.springframework.context.annotation.ClassPathBeanDefinitionScanner
- 所有已实现的接口:
Aware,ResourceLoaderAware,EnvironmentCapable
public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateComponentProvider
A bean definition scanner that detects bean candidates on the classpath, registering corresponding bean definitions with a given registry (BeanFactoryorApplicationContext).Candidate classes are detected through configurable type filters. The default filters include classes that are annotated with Spring's
@Component,@Repository,@Service, or@Controllerstereotype.Also supports Java EE 6's
ManagedBeanand JSR-330'sNamedannotations, if available.- 从以下版本开始:
- 2.5
- 作者:
- Mark Fisher, Juergen Hoeller, Chris Beams
- 另请参阅:
AnnotationConfigApplicationContext.scan(java.lang.String...),Component,Repository,Service,Controller
字段概要
从类继承的字段 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider
logger
构造器概要
构造器 构造器 说明 ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry)Create 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, ResourceLoader resourceLoader)Create a newClassPathBeanDefinitionScannerfor the given bean factory and using the givenEnvironmentwhen evaluating bean definition profile metadata.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 protected 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>doScan(String... basePackages)Perform a scan within the specified base packages, returning the registered bean definitions.BeanDefinitionDefaultsgetBeanDefinitionDefaults()Return the defaults to use for detected beans (nevernull).BeanDefinitionRegistrygetRegistry()Return the BeanDefinitionRegistry that this scanner operates on.protected booleanisCompatible(BeanDefinition newDefinition, BeanDefinition existingDefinition)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.intscan(String... basePackages)Perform a scan within the specified base packages.voidsetAutowireCandidatePatterns(String... autowireCandidatePatterns)Set the name-matching patterns for determining autowire candidates.voidsetBeanDefinitionDefaults(BeanDefinitionDefaults beanDefinitionDefaults)Set the defaults to use for detected beans.voidsetBeanNameGenerator(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(ScopeMetadataResolver scopeMetadataResolver)Set the ScopeMetadataResolver to use for detected bean classes.从类继承的方法 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider
addExcludeFilter, addIncludeFilter, clearCache, findCandidateComponents, getEnvironment, getMetadataReaderFactory, getResourceLoader, isCandidateComponent, isCandidateComponent, registerDefaultFilters, resetFilters, resolveBasePackage, setEnvironment, setMetadataReaderFactory, setResourceLoader, setResourcePattern
构造器详细资料
ClassPathBeanDefinitionScanner
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry)
Create a newClassPathBeanDefinitionScannerfor the given bean factory.- 参数:
registry- theBeanFactoryto load bean definitions into, in the form of aBeanDefinitionRegistry
ClassPathBeanDefinitionScanner
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters)
Create 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.- 参数:
registry- theBeanFactoryto load bean definitions into, in the form of aBeanDefinitionRegistryuseDefaultFilters- whether to include the default filters for the@Component,@Repository,@Service, and@Controllerstereotype annotations- 另请参阅:
ClassPathScanningCandidateComponentProvider.setResourceLoader(org.springframework.core.io.ResourceLoader),ClassPathScanningCandidateComponentProvider.setEnvironment(org.springframework.core.env.Environment)
ClassPathBeanDefinitionScanner
public 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.- 参数:
registry- theBeanFactoryto load bean definitions into, in the form of aBeanDefinitionRegistryuseDefaultFilters- whether to include the default filters for the@Component,@Repository,@Service, and@Controllerstereotype annotationsenvironment- the SpringEnvironmentto use when evaluating bean definition profile metadata- 从以下版本开始:
- 3.1
- 另请参阅:
ClassPathScanningCandidateComponentProvider.setResourceLoader(org.springframework.core.io.ResourceLoader)
ClassPathBeanDefinitionScanner
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment, ResourceLoader resourceLoader)
Create a newClassPathBeanDefinitionScannerfor the given bean factory and using the givenEnvironmentwhen evaluating bean definition profile metadata.- 参数:
registry- theBeanFactoryto load bean definitions into, in the form of aBeanDefinitionRegistryuseDefaultFilters- whether to include the default filters for the@Component,@Repository,@Service, and@Controllerstereotype annotationsenvironment- the SpringEnvironmentto use when evaluating bean definition profile metadataresourceLoader- theResourceLoaderto use- 从以下版本开始:
- 4.3.6
方法详细资料
getRegistry
public final BeanDefinitionRegistry getRegistry()
Return the BeanDefinitionRegistry that this scanner operates on.
setBeanDefinitionDefaults
public void setBeanDefinitionDefaults(BeanDefinitionDefaults beanDefinitionDefaults)
Set the defaults to use for detected beans.
getBeanDefinitionDefaults
public BeanDefinitionDefaults getBeanDefinitionDefaults()
Return the defaults to use for detected beans (nevernull).- 从以下版本开始:
- 4.1
setAutowireCandidatePatterns
public void setAutowireCandidatePatterns(String... autowireCandidatePatterns)
Set the name-matching patterns for determining autowire candidates.- 参数:
autowireCandidatePatterns- the patterns to match against
setBeanNameGenerator
public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
Set the BeanNameGenerator to use for detected bean classes.Default is a
AnnotationBeanNameGenerator.
setScopeMetadataResolver
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.
setScopedProxyMode
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.
setIncludeAnnotationConfig
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.
scan
public int scan(String... basePackages)
Perform a scan within the specified base packages.- 参数:
basePackages- the packages to check for annotated classes- 返回:
- number of beans registered
doScan
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.
- 参数:
basePackages- the packages to check for annotated classes- 返回:
- set of beans registered if any for tooling registration purposes (never
null)
postProcessBeanDefinition
protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, String beanName)
Apply further settings to the given bean definition, beyond the contents retrieved from scanning the component class.- 参数:
beanDefinition- the scanned bean definitionbeanName- the generated bean name for the given bean
registerBeanDefinition
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.
- 参数:
definitionHolder- the bean definition plus bean name for the beanregistry- the BeanDefinitionRegistry to register the bean with
checkCandidate
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.- 参数:
beanName- the suggested name for the beanbeanDefinition- the corresponding bean definition- 返回:
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- 抛出:
org.springframework.context.annotation.ConflictingBeanDefinitionException- if an existing, incompatible bean definition has been found for the specified nameIllegalStateException
isCompatible
protected boolean isCompatible(BeanDefinition newDefinition, BeanDefinition existingDefinition)
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.
- 参数:
newDefinition- the new bean definition, originated from scanningexistingDefinition- the existing bean definition, potentially an explicitly defined one or a previously generated one from scanning- 返回:
- whether the definitions are considered as compatible, with the new definition to be skipped in favor of the existing definition