类 ClassPathScanningCandidateComponentProvider
- java.lang.Object
- org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider
- 所有已实现的接口:
Aware
,ResourceLoaderAware
,EnvironmentCapable
public class ClassPathScanningCandidateComponentProvider extends Object implements EnvironmentCapable, ResourceLoaderAware
A component provider that provides candidate components from a base package. Can usethe index
if it is available of scans the classpath otherwise. Candidate components are identified by applying exclude and include filters.AnnotationTypeFilter
,AssignableTypeFilter
include filters on an annotation/superclass that are annotated withIndexed
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 ASMClassReader
.- 从以下版本开始:
- 2.5
- 作者:
- Mark Fisher, Juergen Hoeller, Ramnivas Laddad, Chris Beams, Stephane Nicoll
- 另请参阅:
MetadataReaderFactory
,AnnotationMetadata
,ScannedGenericBeanDefinition
,CandidateComponentsIndex
构造器概要
构造器 限定符 构造器 说明 protected
ClassPathScanningCandidateComponentProvider()
Protected constructor for flexible subclass initialization.ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)
Create a ClassPathScanningCandidateComponentProvider with aStandardEnvironment
.ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment)
Create a ClassPathScanningCandidateComponentProvider with the givenEnvironment
.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
addExcludeFilter(TypeFilter excludeFilter)
Add an exclude type filter to the front of the exclusion list.void
addIncludeFilter(TypeFilter includeFilter)
Add an include type filter to the end of the inclusion list.void
clearCache()
Clear the local metadata cache, if any, removing all cached class metadata.Set<BeanDefinition>
findCandidateComponents(String basePackage)
Scan the class path for candidate components.Environment
getEnvironment()
Return theEnvironment
associated with this component.MetadataReaderFactory
getMetadataReaderFactory()
Return the MetadataReaderFactory used by this component provider.protected BeanDefinitionRegistry
getRegistry()
Return theBeanDefinitionRegistry
used by this scanner, if any.ResourceLoader
getResourceLoader()
Return the ResourceLoader that this component provider uses.protected boolean
isCandidateComponent(AnnotatedBeanDefinition beanDefinition)
Determine whether the given bean definition qualifies as candidate.protected boolean
isCandidateComponent(MetadataReader metadataReader)
Determine whether the given class does not match any exclude filter and does match at least one include filter.protected void
registerDefaultFilters()
Register the default filter for@Component
.void
resetFilters(boolean useDefaultFilters)
Reset the configured type filters.protected String
resolveBasePackage(String basePackage)
Resolve the specified base package into a pattern specification for the package search path.void
setEnvironment(Environment environment)
Set the Environment to use when resolving placeholders and evaluating@Conditional
-annotated component classes.void
setMetadataReaderFactory(MetadataReaderFactory metadataReaderFactory)
Set theMetadataReaderFactory
to use.void
setResourceLoader(ResourceLoader resourceLoader)
Set theResourceLoader
to use for resource locations.void
setResourcePattern(String resourcePattern)
Set the resource pattern to use when scanning the classpath.
构造器详细资料
ClassPathScanningCandidateComponentProvider
protected ClassPathScanningCandidateComponentProvider()
Protected constructor for flexible subclass initialization.- 从以下版本开始:
- 4.3.6
ClassPathScanningCandidateComponentProvider
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)
Create a ClassPathScanningCandidateComponentProvider with aStandardEnvironment
.- 参数:
useDefaultFilters
- whether to register the default filters for the@Component
,@Repository
,@Service
, and@Controller
stereotype annotations- 另请参阅:
registerDefaultFilters()
ClassPathScanningCandidateComponentProvider
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment)
Create a ClassPathScanningCandidateComponentProvider with the givenEnvironment
.- 参数:
useDefaultFilters
- whether to register the default filters for the@Component
,@Repository
,@Service
, and@Controller
stereotype annotationsenvironment
- the Environment to use- 另请参阅:
registerDefaultFilters()
方法详细资料
setResourcePattern
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.
addIncludeFilter
public void addIncludeFilter(TypeFilter includeFilter)
Add an include type filter to the end of the inclusion list.
addExcludeFilter
public void addExcludeFilter(TypeFilter excludeFilter)
Add an exclude type filter to the front of the exclusion list.
resetFilters
public void resetFilters(boolean useDefaultFilters)
Reset the configured type filters.- 参数:
useDefaultFilters
- whether to re-register the default filters for the@Component
,@Repository
,@Service
, and@Controller
stereotype annotations- 另请参阅:
registerDefaultFilters()
registerDefaultFilters
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 Java EE 6's
ManagedBean
and JSR-330'sNamed
annotations, if available.
setEnvironment
public void setEnvironment(Environment environment)
Set the Environment to use when resolving placeholders and evaluating@Conditional
-annotated component classes.The default is a
StandardEnvironment
.- 参数:
environment
- the Environment to use
getEnvironment
public final Environment getEnvironment()
从接口复制的说明:EnvironmentCapable
Return theEnvironment
associated with this component.- 指定者:
getEnvironment
在接口中EnvironmentCapable
getRegistry
@Nullable protected BeanDefinitionRegistry getRegistry()
Return theBeanDefinitionRegistry
used by this scanner, if any.
setResourceLoader
public void setResourceLoader(@Nullable ResourceLoader resourceLoader)
Set theResourceLoader
to use for resource locations. This will typically be aResourcePatternResolver
implementation.Default is a
PathMatchingResourcePatternResolver
, also capable of resource pattern resolving through theResourcePatternResolver
interface.- 指定者:
setResourceLoader
在接口中ResourceLoaderAware
- 参数:
resourceLoader
- the ResourceLoader object to be used by this object- 另请参阅:
ResourcePatternResolver
,PathMatchingResourcePatternResolver
getResourceLoader
public final ResourceLoader getResourceLoader()
Return the ResourceLoader that this component provider uses.
setMetadataReaderFactory
public void setMetadataReaderFactory(MetadataReaderFactory metadataReaderFactory)
Set theMetadataReaderFactory
to use.Default is a
CachingMetadataReaderFactory
for the specified resource loader.Call this setter method after
setResourceLoader(org.springframework.core.io.ResourceLoader)
in order for the given MetadataReaderFactory to override the default factory.
getMetadataReaderFactory
public final MetadataReaderFactory getMetadataReaderFactory()
Return the MetadataReaderFactory used by this component provider.
findCandidateComponents
public Set<BeanDefinition> findCandidateComponents(String basePackage)
Scan the class path for candidate components.- 参数:
basePackage
- the package to check for annotated classes- 返回:
- a corresponding Set of autodetected bean definitions
resolveBasePackage
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.
- 参数:
basePackage
- the base package as specified by the user- 返回:
- the pattern specification to be used for package searching
isCandidateComponent
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.- 参数:
metadataReader
- the ASM ClassReader for the class- 返回:
- whether the class qualifies as a candidate component
- 抛出:
IOException
isCandidateComponent
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.
- 参数:
beanDefinition
- the bean definition to check- 返回:
- whether the bean definition qualifies as a candidate component
clearCache
public void clearCache()
Clear the local metadata cache, if any, removing all cached class metadata.