Class ClassPathScanningCandidateComponentProvider

java.lang.Object
org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider
All Implemented Interfaces:
org.springframework.beans.factory.Aware, ResourceLoaderAware, org.springframework.core.env.EnvironmentCapable
Direct Known Subclasses:
ClassPathBeanDefinitionScanner

public class ClassPathScanningCandidateComponentProvider extends Object implements org.springframework.core.env.EnvironmentCapable, ResourceLoaderAware
A component provider that scans for candidate components starting from a specified base package. Can use the component index, if it is available, and scans the classpath otherwise.

Candidate components are identified by applying exclude and include filters. AnnotationTypeFilter and AssignableTypeFilter include filters for an annotation/target-type that is 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.

Since:
2.5
Author:
Mark Fisher, Juergen Hoeller, Ramnivas Laddad, Chris Beams, Stephane Nicoll, Sam Brannen
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final org.apache.commons.logging.Log
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Protected constructor for flexible subclass initialization.
     
    Create a ClassPathScanningCandidateComponentProvider with a StandardEnvironment.
     
    ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, org.springframework.core.env.Environment environment)
    Create a ClassPathScanningCandidateComponentProvider with the given Environment.
  • Method Summary

    Modifier and Type
    Method
    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
    Clear the local metadata cache, if any, removing all cached class metadata.
    Set<org.springframework.beans.factory.config.BeanDefinition>
    Scan the component index or class path for candidate components.
    final org.springframework.core.env.Environment
     
    final org.springframework.core.type.classreading.MetadataReaderFactory
    Return the MetadataReaderFactory used by this component provider.
    protected org.springframework.beans.factory.support.BeanDefinitionRegistry
    Return the BeanDefinitionRegistry used by this scanner, if any.
    final org.springframework.core.io.ResourceLoader
    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
    Register the default filter for @Component.
    void
    resetFilters(boolean useDefaultFilters)
    Reset the configured type filters.
    protected String
    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 @Conditional-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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • logger

      protected final org.apache.commons.logging.Log logger
  • Constructor Details

    • ClassPathScanningCandidateComponentProvider

      protected ClassPathScanningCandidateComponentProvider()
      Protected constructor for flexible subclass initialization.
      Since:
      4.3.6
    • ClassPathScanningCandidateComponentProvider

      public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters)
      Create a ClassPathScanningCandidateComponentProvider with a StandardEnvironment.
      Parameters:
      useDefaultFilters - whether to register the default filters for the @Component, @Repository, @Service, and @Controller stereotype annotations
      See Also:
    • ClassPathScanningCandidateComponentProvider

      public ClassPathScanningCandidateComponentProvider(boolean useDefaultFilters, org.springframework.core.env.Environment environment)
      Create a ClassPathScanningCandidateComponentProvider with the given Environment.
      Parameters:
      useDefaultFilters - whether to register the default filters for the @Component, @Repository, @Service, and @Controller stereotype annotations
      environment - the Environment to use
      See Also:
  • Method Details

    • 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.
      See Also:
    • addIncludeFilter

      public void addIncludeFilter(org.springframework.core.type.filter.TypeFilter includeFilter)
      Add an include type filter to the end of the inclusion list.
    • addExcludeFilter

      public void addExcludeFilter(org.springframework.core.type.filter.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 @Controller stereotype annotations
      See Also:
    • 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 Jakarta EE's ManagedBean and JSR-330's Named annotations, if available.

    • setEnvironment

      public void setEnvironment(org.springframework.core.env.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 org.springframework.core.env.Environment getEnvironment()
      Specified by:
      getEnvironment in interface org.springframework.core.env.EnvironmentCapable
    • getRegistry

      @Nullable protected org.springframework.beans.factory.support.BeanDefinitionRegistry getRegistry()
      Return the BeanDefinitionRegistry used by this scanner, if any.
    • setResourceLoader

      public void setResourceLoader(@Nullable org.springframework.core.io.ResourceLoader resourceLoader)
      Set the 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.

      Specified by:
      setResourceLoader in interface ResourceLoaderAware
      Parameters:
      resourceLoader - the ResourceLoader object to be used by this object
      See Also:
      • ResourcePatternResolver
      • PathMatchingResourcePatternResolver
    • getResourceLoader

      public final org.springframework.core.io.ResourceLoader getResourceLoader()
      Return the ResourceLoader that this component provider uses.
    • setMetadataReaderFactory

      public void setMetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory metadataReaderFactory)
      Set the 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.

    • getMetadataReaderFactory

      public final org.springframework.core.type.classreading.MetadataReaderFactory getMetadataReaderFactory()
      Return the MetadataReaderFactory used by this component provider.
    • findCandidateComponents

      public Set<org.springframework.beans.factory.config.BeanDefinition> findCandidateComponents(String basePackage)
      Scan the component index or 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(org.springframework.core.type.classreading.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(org.springframework.beans.factory.annotation.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 local metadata cache, if any, removing all cached class metadata.