public class ClassPathScanningCandidateComponentProvider extends Object implements org.springframework.core.env.EnvironmentCapable, ResourceLoaderAware
This implementation is based on Spring's
MetadataReader
facility, backed by an ASM ClassReader
.
MetadataReaderFactory
,
AnnotationMetadata
,
ScannedGenericBeanDefinition
Constructor and Description |
---|
ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)
Create a ClassPathScanningCandidateComponentProvider.
|
ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters,
org.springframework.core.env.Environment environment) |
Modifier and Type | Method and Description |
---|---|
void |
addExcludeFilter(org.springframework.core.type.filter.TypeFilter excludeFilter)
Add an exclude type filter to the front of the exclusion list.
|
void |
addIncludeFilter(org.springframework.core.type.filter.TypeFilter includeFilter)
Add an include type filter to the end of the inclusion list.
|
void |
clearCache()
Clear the underlying metadata cache, removing all cached class metadata.
|
Set<org.springframework.beans.factory.config.BeanDefinition> |
findCandidateComponents(String basePackage)
Scan the class path for candidate components.
|
org.springframework.core.env.Environment |
getEnvironment() |
org.springframework.core.type.classreading.MetadataReaderFactory |
getMetadataReaderFactory()
Return the MetadataReaderFactory used by this component provider.
|
org.springframework.core.io.ResourceLoader |
getResourceLoader()
Return the ResourceLoader that this component provider uses.
|
protected boolean |
isCandidateComponent(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition beanDefinition)
Determine whether the given bean definition qualifies as candidate.
|
protected boolean |
isCandidateComponent(org.springframework.core.type.classreading.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(org.springframework.core.env.Environment environment)
Set the Environment to use when resolving placeholders and evaluating
@Profile -annotated component classes. |
void |
setMetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory metadataReaderFactory)
Set the
MetadataReaderFactory to use. |
void |
setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
Set the ResourceLoader to use for resource locations.
|
void |
setResourcePattern(String resourcePattern)
Set the resource pattern to use when scanning the classpath.
|
protected final Log logger
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)
useDefaultFilters
- whether to register the default filters for the
@Component
, @Repository
,
@Service
, and @Controller
stereotype annotationsregisterDefaultFilters()
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, org.springframework.core.env.Environment environment)
public void setResourceLoader(org.springframework.core.io.ResourceLoader resourceLoader)
Default is PathMatchingResourcePatternResolver, also capable of resource pattern resolving through the ResourcePatternResolver interface.
setResourceLoader
in interface ResourceLoaderAware
resourceLoader
- ResourceLoader object to be used by this objectResourcePatternResolver
,
PathMatchingResourcePatternResolver
public final org.springframework.core.io.ResourceLoader getResourceLoader()
public void setMetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory metadataReaderFactory)
MetadataReaderFactory
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.
public final org.springframework.core.type.classreading.MetadataReaderFactory getMetadataReaderFactory()
public void setEnvironment(org.springframework.core.env.Environment environment)
@Profile
-annotated component classes.
The default is a StandardEnvironment
environment
- the Environment to usepublic final org.springframework.core.env.Environment getEnvironment()
getEnvironment
in interface org.springframework.core.env.EnvironmentCapable
public void setResourcePattern(String resourcePattern)
public void addIncludeFilter(org.springframework.core.type.filter.TypeFilter includeFilter)
public void addExcludeFilter(org.springframework.core.type.filter.TypeFilter excludeFilter)
public void resetFilters(boolean useDefaultFilters)
useDefaultFilters
- whether to re-register the default filters for
the @Component
, @Repository
,
@Service
, and @Controller
stereotype annotationsregisterDefaultFilters()
protected void registerDefaultFilters()
@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's Named
annotations, if available.
public Set<org.springframework.beans.factory.config.BeanDefinition> findCandidateComponents(String basePackage)
basePackage
- the package to check for annotated classesprotected String resolveBasePackage(String basePackage)
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 userprotected boolean isCandidateComponent(org.springframework.core.type.classreading.MetadataReader metadataReader) throws IOException
metadataReader
- the ASM ClassReader for the classIOException
protected boolean isCandidateComponent(org.springframework.beans.factory.annotation.AnnotatedBeanDefinition beanDefinition)
The default implementation checks whether the class is concrete (i.e. not abstract and not an interface). Can be overridden in subclasses.
beanDefinition
- the bean definition to checkpublic void clearCache()