public class ClassPathScanningCandidateComponentProvider extends java.lang.Object implements EnvironmentCapable, ResourceLoaderAware
the 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 with Indexed
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 ASM ClassReader
.
MetadataReaderFactory
,
AnnotationMetadata
,
ScannedGenericBeanDefinition
,
CandidateComponentsIndex
Modifier and Type | Field and Description |
---|---|
private CandidateComponentsIndex |
componentsIndex |
private ConditionEvaluator |
conditionEvaluator |
(package private) static java.lang.String |
DEFAULT_RESOURCE_PATTERN |
private Environment |
environment |
private java.util.List<TypeFilter> |
excludeFilters |
private java.util.List<TypeFilter> |
includeFilters |
protected Log |
logger |
private MetadataReaderFactory |
metadataReaderFactory |
private java.lang.String |
resourcePattern |
private ResourcePatternResolver |
resourcePatternResolver |
Modifier | Constructor and Description |
---|---|
protected |
ClassPathScanningCandidateComponentProvider()
Protected constructor for flexible subclass initialization.
|
|
ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)
Create a ClassPathScanningCandidateComponentProvider with a
StandardEnvironment . |
|
ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters,
Environment environment)
Create a ClassPathScanningCandidateComponentProvider with the given
Environment . |
Modifier and Type | Method and Description |
---|---|
private java.util.Set<BeanDefinition> |
addCandidateComponentsFromIndex(java.lang.String basePackage) |
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.
|
protected java.lang.String |
extractStereotype(TypeFilter filter)
Extract the stereotype to use for the specified compatible filter.
|
java.util.Set<BeanDefinition> |
findCandidateComponents(java.lang.String basePackage)
Scan the class path for candidate components.
|
Environment |
getEnvironment()
Return the
Environment associated with this component. |
MetadataReaderFactory |
getMetadataReaderFactory()
Return the MetadataReaderFactory used by this component provider.
|
protected BeanDefinitionRegistry |
getRegistry()
Return the
BeanDefinitionRegistry 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.
|
private boolean |
isConditionMatch(MetadataReader metadataReader)
Determine whether the given class is a candidate component based on any
@Conditional annotations. |
protected boolean |
isIndexSupported()
Determine if the index can be used by this instance.
|
protected boolean |
isIndexSupportsIncludeFilter(TypeFilter filter)
Determine if the specified include
TypeFilter is supported by the index. |
protected void |
registerDefaultFilters()
Register the default filter for
@Component . |
void |
resetFilters(boolean useDefaultFilters)
Reset the configured type filters.
|
protected java.lang.String |
resolveBasePackage(java.lang.String basePackage)
Resolve the specified base package into a pattern specification for
the package search path.
|
private java.util.Set<BeanDefinition> |
scanCandidateComponents(java.lang.String basePackage) |
void |
setEnvironment(Environment environment)
Set the Environment to use when resolving placeholders and evaluating
@Conditional -annotated component classes. |
void |
setMetadataReaderFactory(MetadataReaderFactory metadataReaderFactory)
Set the
MetadataReaderFactory to use. |
void |
setResourceLoader(ResourceLoader resourceLoader)
Set the
ResourceLoader to use for resource locations. |
void |
setResourcePattern(java.lang.String resourcePattern)
Set the resource pattern to use when scanning the classpath.
|
static final java.lang.String DEFAULT_RESOURCE_PATTERN
protected final Log logger
private java.lang.String resourcePattern
private final java.util.List<TypeFilter> includeFilters
private final java.util.List<TypeFilter> excludeFilters
private Environment environment
private ConditionEvaluator conditionEvaluator
private ResourcePatternResolver resourcePatternResolver
private MetadataReaderFactory metadataReaderFactory
private CandidateComponentsIndex componentsIndex
protected ClassPathScanningCandidateComponentProvider()
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)
StandardEnvironment
.useDefaultFilters
- whether to register the default filters for the
@Component
, @Repository
,
@Service
, and @Controller
stereotype annotationsregisterDefaultFilters()
public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, Environment environment)
Environment
.useDefaultFilters
- whether to register the default filters for the
@Component
, @Repository
,
@Service
, and @Controller
stereotype annotationsenvironment
- the Environment to useregisterDefaultFilters()
public void setResourcePattern(java.lang.String resourcePattern)
public void addIncludeFilter(TypeFilter includeFilter)
public void addExcludeFilter(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 javax.annotation.ManagedBean
and
JSR-330's javax.inject.Named
annotations, if available.
public void setEnvironment(Environment environment)
@Conditional
-annotated component classes.
The default is a StandardEnvironment
.
environment
- the Environment to usepublic final Environment getEnvironment()
EnvironmentCapable
Environment
associated with this component.getEnvironment
in interface EnvironmentCapable
protected BeanDefinitionRegistry getRegistry()
BeanDefinitionRegistry
used by this scanner, if any.public void setResourceLoader(ResourceLoader resourceLoader)
ResourceLoader
to use for resource locations.
This will typically be a ResourcePatternResolver
implementation.
Default is a 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 ResourceLoader getResourceLoader()
public void setMetadataReaderFactory(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 MetadataReaderFactory getMetadataReaderFactory()
public java.util.Set<BeanDefinition> findCandidateComponents(java.lang.String basePackage)
basePackage
- the package to check for annotated classesprotected boolean isIndexSupported()
true
if the index is available and the configuration of this
instance is supported by it, false
otherwiseprotected boolean isIndexSupportsIncludeFilter(TypeFilter filter)
TypeFilter
is supported by the index.filter
- the filter to checkextractStereotype(TypeFilter)
protected java.lang.String extractStereotype(TypeFilter filter)
filter
- the filter to handleisIndexSupportsIncludeFilter(TypeFilter)
private java.util.Set<BeanDefinition> addCandidateComponentsFromIndex(java.lang.String basePackage)
private java.util.Set<BeanDefinition> scanCandidateComponents(java.lang.String basePackage)
protected java.lang.String resolveBasePackage(java.lang.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(MetadataReader metadataReader) throws java.io.IOException
metadataReader
- the ASM ClassReader for the classjava.io.IOException
private boolean isConditionMatch(MetadataReader metadataReader)
@Conditional
annotations.metadataReader
- the ASM ClassReader for the classprotected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition)
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 checkpublic void clearCache()