Class ClassPathScanningCandidateComponentProvider
- java.lang.Object
- org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider
- All Implemented Interfaces:
Aware,ResourceLoaderAware,EnvironmentCapable
- Direct Known Subclasses:
ClassPathBeanDefinitionScanner
public class ClassPathScanningCandidateComponentProvider extends Object implements EnvironmentCapable, ResourceLoaderAware
A component provider that scans the classpath from a base package. It then applies exclude and include filters to the resulting classes to find candidates.This implementation is based on Spring's
MetadataReaderfacility, backed by an ASMClassReader.- Since:
- 2.5
- Author:
- Mark Fisher, Juergen Hoeller, Ramnivas Laddad, Chris Beams
- See Also:
MetadataReaderFactory,AnnotationMetadata,ScannedGenericBeanDefinition
Constructor Summary
Constructors Modifier Constructor Description protectedClassPathScanningCandidateComponentProvider()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.
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddExcludeFilter(TypeFilter excludeFilter)Add an exclude type filter to the front of the exclusion list.voidaddIncludeFilter(TypeFilter includeFilter)Add an include type filter to the end of the inclusion list.voidclearCache()Clear the underlying metadata cache, removing all cached class metadata.Set<BeanDefinition>findCandidateComponents(String basePackage)Scan the class path for candidate components.EnvironmentgetEnvironment()Return theEnvironmentassociated with this component (may benullor a default environment).MetadataReaderFactorygetMetadataReaderFactory()Return the MetadataReaderFactory used by this component provider.protected BeanDefinitionRegistrygetRegistry()Return theBeanDefinitionRegistryused by this scanner, if any.ResourceLoadergetResourceLoader()Return the ResourceLoader that this component provider uses.protected booleanisCandidateComponent(AnnotatedBeanDefinition beanDefinition)Determine whether the given bean definition qualifies as candidate.protected booleanisCandidateComponent(MetadataReader metadataReader)Determine whether the given class does not match any exclude filter and does match at least one include filter.protected voidregisterDefaultFilters()Register the default filter for@Component.voidresetFilters(boolean useDefaultFilters)Reset the configured type filters.protected StringresolveBasePackage(String basePackage)Resolve the specified base package into a pattern specification for the package search path.voidsetEnvironment(Environment environment)Set the Environment to use when resolving placeholders and evaluating@Conditional-annotated component classes.voidsetMetadataReaderFactory(MetadataReaderFactory metadataReaderFactory)Set theMetadataReaderFactoryto use.voidsetResourceLoader(ResourceLoader resourceLoader)Set theResourceLoaderto use for resource locations.voidsetResourcePattern(String resourcePattern)Set the resource pattern to use when scanning the classpath.
Constructor Detail
ClassPathScanningCandidateComponentProvider
protected ClassPathScanningCandidateComponentProvider()
Protected constructor for flexible subclass initialization.- Since:
- 4.3.6
ClassPathScanningCandidateComponentProvider
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)
Create a ClassPathScanningCandidateComponentProvider with aStandardEnvironment.- Parameters:
useDefaultFilters- whether to register the default filters for the@Component,@Repository,@Service, and@Controllerstereotype annotations- See Also:
registerDefaultFilters()
ClassPathScanningCandidateComponentProvider
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment)
Create a ClassPathScanningCandidateComponentProvider with the givenEnvironment.- Parameters:
useDefaultFilters- whether to register the default filters for the@Component,@Repository,@Service, and@Controllerstereotype annotationsenvironment- the Environment to use- See Also:
registerDefaultFilters()
Method Detail
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.- Parameters:
useDefaultFilters- whether to re-register the default filters for the@Component,@Repository,@Service, and@Controllerstereotype annotations- See Also:
registerDefaultFilters()
registerDefaultFilters
protected void registerDefaultFilters()
Register the default filter for@Component.This will implicitly register all annotations that have the
@Componentmeta-annotation including the@Repository,@Service, and@Controllerstereotype annotations.Also supports Java EE 6's
ManagedBeanand JSR-330'sNamedannotations, 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.- Parameters:
environment- the Environment to use
getEnvironment
public final Environment getEnvironment()
Description copied from interface:EnvironmentCapableReturn theEnvironmentassociated with this component (may benullor a default environment).- Specified by:
getEnvironmentin interfaceEnvironmentCapable
getRegistry
protected BeanDefinitionRegistry getRegistry()
Return theBeanDefinitionRegistryused by this scanner, if any.
setResourceLoader
public void setResourceLoader(ResourceLoader resourceLoader)
Set theResourceLoaderto use for resource locations. This will typically be aResourcePatternResolverimplementation.Default is a
PathMatchingResourcePatternResolver, also capable of resource pattern resolving through theResourcePatternResolverinterface.- Specified by:
setResourceLoaderin interfaceResourceLoaderAware- Parameters:
resourceLoader- the ResourceLoader object to be used by this object- See Also:
ResourcePatternResolver,PathMatchingResourcePatternResolver
getResourceLoader
public final ResourceLoader getResourceLoader()
Return the ResourceLoader that this component provider uses.
setMetadataReaderFactory
public void setMetadataReaderFactory(MetadataReaderFactory metadataReaderFactory)
Set theMetadataReaderFactoryto use.Default is a
CachingMetadataReaderFactoryfor 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.- Parameters:
basePackage- the package to check for annotated classes- Returns:
- 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.
- Parameters:
basePackage- the base package as specified by the user- Returns:
- 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.- Parameters:
metadataReader- the ASM ClassReader for the class- Returns:
- whether the class qualifies as a candidate component
- Throws:
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.
- Parameters:
beanDefinition- the bean definition to check- Returns:
- whether the bean definition qualifies as a candidate component
clearCache
public void clearCache()
Clear the underlying metadata cache, removing all cached class metadata.