org.springframework.context.annotation

## 类 ClassPathBeanDefinitionScanner

- java.lang.Object
- - org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider
  - - org.springframework.context.annotation.ClassPathBeanDefinitionScanner

- 所有已实现的接口:
    org.springframework.beans.factory.Aware, ResourceLoaderAware, org.springframework.core.env.EnvironmentCapable

      public class ClassPathBeanDefinitionScanner
      extends ClassPathScanningCandidateComponentProvider

  A bean definition scanner that detects bean candidates on the classpath, registering corresponding bean definitions with a given registry (`BeanFactory` or `ApplicationContext`).

  Candidate classes are detected through configurable type filters. The default filters include classes that are annotated with Spring's `@Component`, `@Repository`, `@Service`, or `@Controller` stereotype.

  Also supports Java EE 6's `ManagedBean` and JSR-330's `Named` annotations, if available.

  从以下版本开始:
    2.5
  作者:
    Mark Fisher, Juergen Hoeller, Chris Beams
  另请参阅:
    `AnnotationConfigApplicationContext.scan(java.lang.String...)`, `Component`, `Repository`, `Service`, `Controller`

- - ### 字段概要

    - ### 从类继承的字段 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

      `logger`

  - ### 构造器概要

    构造器
    构造器和说明
    `ClassPathBeanDefinitionScanner(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)` Create a new `ClassPathBeanDefinitionScanner` for the given bean factory.
    `ClassPathBeanDefinitionScanner(org.springframework.beans.factory.support.BeanDefinitionRegistry registry, boolean useDefaultFilters)` Create a new `ClassPathBeanDefinitionScanner` for the given bean factory.
    `ClassPathBeanDefinitionScanner(org.springframework.beans.factory.support.BeanDefinitionRegistry registry, boolean useDefaultFilters, org.springframework.core.env.Environment environment)` Create a new `ClassPathBeanDefinitionScanner` for the given bean factory and using the given `Environment` when evaluating bean definition profile metadata.
    `ClassPathBeanDefinitionScanner(org.springframework.beans.factory.support.BeanDefinitionRegistry registry, boolean useDefaultFilters, org.springframework.core.env.Environment environment, org.springframework.core.io.ResourceLoader resourceLoader)`
    ClassPathBeanDefinitionScanner就是用来扫描我们classpath下的标注了@Service @Compent @Respository @Controller 注解

  - ### 方法概要

    所有方法 实例方法 具体方法
    限定符和类型 | 方法和说明
    --- | ---
    `protected boolean` | `checkCandidate(String beanName, org.springframework.beans.factory.config.BeanDefinition beanDefinition)` Check the given candidate's bean name, determining whether the corresponding bean definition needs to be registered or conflicts with an existing definition.
    `protected Set<org.springframework.beans.factory.config.BeanDefinitionHolder>` | `doScan(String... basePackages)` Perform a scan within the specified base packages, returning the registered bean definitions.
    `org.springframework.beans.factory.support.BeanDefinitionDefaults` | `getBeanDefinitionDefaults()` Return the defaults to use for detected beans (never `null`).
    `org.springframework.beans.factory.support.BeanDefinitionRegistry` | `getRegistry()` Return the BeanDefinitionRegistry that this scanner operates on.
    `protected boolean` | `isCompatible(org.springframework.beans.factory.config.BeanDefinition newDefinition, org.springframework.beans.factory.config.BeanDefinition existingDefinition)` Determine whether the given new bean definition is compatible with the given existing bean definition.
    `protected void` | `postProcessBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition beanDefinition, String beanName)` Apply further settings to the given bean definition, beyond the contents retrieved from scanning the component class.
    `protected void` | `registerBeanDefinition(org.springframework.beans.factory.config.BeanDefinitionHolder definitionHolder, org.springframework.beans.factory.support.BeanDefinitionRegistry registry)` Register the specified bean with the given registry.
    `int` | `scan(String... basePackages)` 方法实现说明:真正的扫描我们@MapperScan的backpackage指定的路径的 Mapper包
    `void` | `setAutowireCandidatePatterns(String... autowireCandidatePatterns)` Set the name-matching patterns for determining autowire candidates.
    `void` | `setBeanDefinitionDefaults(org.springframework.beans.factory.support.BeanDefinitionDefaults beanDefinitionDefaults)` Set the defaults to use for detected beans.
    `void` | `setBeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator beanNameGenerator)` Set the BeanNameGenerator to use for detected bean classes.
    `void` | `setIncludeAnnotationConfig(boolean includeAnnotationConfig)` Specify whether to register annotation config post-processors.
    `void` | `setScopedProxyMode(ScopedProxyMode scopedProxyMode)` Specify the proxy behavior for non-singleton scoped beans.
    `void` | `setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)` Set the ScopeMetadataResolver to use for detected bean classes.

    - ### 从类继承的方法 org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider

      `addExcludeFilter, addIncludeFilter, clearCache, findCandidateComponents, getEnvironment, getMetadataReaderFactory, getResourceLoader, isCandidateComponent, isCandidateComponent, registerDefaultFilters, resetFilters, resolveBasePackage, setEnvironment, setMetadataReaderFactory, setResourceLoader, setResourcePattern`

    - ### 从类继承的方法 java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### 构造器详细资料

    - #### ClassPathBeanDefinitionScanner

          public ClassPathBeanDefinitionScanner(org.springframework.beans.factory.support.BeanDefinitionRegistry registry)

      Create a new `ClassPathBeanDefinitionScanner` for the given bean factory.
      参数:
        `registry` - the `BeanFactory` to load bean definitions into, in the form of a `BeanDefinitionRegistry`

    - #### ClassPathBeanDefinitionScanner

          public ClassPathBeanDefinitionScanner(org.springframework.beans.factory.support.BeanDefinitionRegistry registry,
                                                boolean useDefaultFilters)

      Create a new `ClassPathBeanDefinitionScanner` for the given bean factory.

      If the passed-in bean factory does not only implement the `BeanDefinitionRegistry` interface but also the `ResourceLoader` interface, it will be used as default `ResourceLoader` as well. This will usually be the case for `ApplicationContext` implementations.

      If given a plain `BeanDefinitionRegistry`, the default `ResourceLoader` will be a `PathMatchingResourcePatternResolver`.

      If the passed-in bean factory also implements `EnvironmentCapable` its environment will be used by this reader. Otherwise, the reader will initialize and use a `StandardEnvironment`. All `ApplicationContext` implementations are `EnvironmentCapable`, while normal `BeanFactory` implementations are not.

      参数:
        `registry` - the `BeanFactory` to load bean definitions into, in the form of a `BeanDefinitionRegistry`
        `useDefaultFilters` - whether to include the default filters for the `@Component`, `@Repository`, `@Service`, and `@Controller` stereotype annotations
      另请参阅:
        `ClassPathScanningCandidateComponentProvider.setResourceLoader(org.springframework.core.io.ResourceLoader)`, `ClassPathScanningCandidateComponentProvider.setEnvironment(org.springframework.core.env.Environment)`

    - #### ClassPathBeanDefinitionScanner

          public ClassPathBeanDefinitionScanner(org.springframework.beans.factory.support.BeanDefinitionRegistry registry,
                                                boolean useDefaultFilters,
                                                org.springframework.core.env.Environment environment)

      Create a new `ClassPathBeanDefinitionScanner` for the given bean factory and using the given `Environment` when evaluating bean definition profile metadata.

      If the passed-in bean factory does not only implement the `BeanDefinitionRegistry` interface but also the `ResourceLoader` interface, it will be used as default `ResourceLoader` as well. This will usually be the case for `ApplicationContext` implementations.

      If given a plain `BeanDefinitionRegistry`, the default `ResourceLoader` will be a `PathMatchingResourcePatternResolver`.

      参数:
        `registry` - the `BeanFactory` to load bean definitions into, in the form of a `BeanDefinitionRegistry`
        `useDefaultFilters` - whether to include the default filters for the `@Component`, `@Repository`, `@Service`, and `@Controller` stereotype annotations
        `environment` - the Spring `Environment` to use when evaluating bean definition profile metadata
      从以下版本开始:
        3.1
      另请参阅:
        `ClassPathScanningCandidateComponentProvider.setResourceLoader(org.springframework.core.io.ResourceLoader)`

    - #### ClassPathBeanDefinitionScanner

          public ClassPathBeanDefinitionScanner(org.springframework.beans.factory.support.BeanDefinitionRegistry registry,
                                                boolean useDefaultFilters,
                                                org.springframework.core.env.Environment environment,
                                                @Nullable
                                                org.springframework.core.io.ResourceLoader resourceLoader)

      ClassPathBeanDefinitionScanner就是用来扫描我们classpath下的标注了@Service @Compent @Respository @Controller 注解
      参数:
        `environment` - 环境对象 definition profile metadata
        `resourceLoader` - the `ResourceLoader` to use
      从以下版本开始:
        4.3.6

  - ### 方法详细资料

    - #### getRegistry

          public final org.springframework.beans.factory.support.BeanDefinitionRegistry getRegistry()

      Return the BeanDefinitionRegistry that this scanner operates on.
      覆盖:
        `getRegistry` 在类中 `ClassPathScanningCandidateComponentProvider`

    - #### setBeanDefinitionDefaults

          public void setBeanDefinitionDefaults(@Nullable
                                                org.springframework.beans.factory.support.BeanDefinitionDefaults beanDefinitionDefaults)

      Set the defaults to use for detected beans.
      另请参阅:
        `BeanDefinitionDefaults`

    - #### getBeanDefinitionDefaults

          public org.springframework.beans.factory.support.BeanDefinitionDefaults getBeanDefinitionDefaults()

      Return the defaults to use for detected beans (never `null`).
      从以下版本开始:
        4.1

    - #### setAutowireCandidatePatterns

          public void setAutowireCandidatePatterns(@Nullable
                                                   String... autowireCandidatePatterns)

      Set the name-matching patterns for determining autowire candidates.
      参数:
        `autowireCandidatePatterns` - the patterns to match against

    - #### setBeanNameGenerator

          public void setBeanNameGenerator(@Nullable
                                           org.springframework.beans.factory.support.BeanNameGenerator beanNameGenerator)

      Set the BeanNameGenerator to use for detected bean classes.

      Default is a `AnnotationBeanNameGenerator`.

    - #### setScopeMetadataResolver

          public void setScopeMetadataResolver(@Nullable
                                               ScopeMetadataResolver scopeMetadataResolver)

      Set the ScopeMetadataResolver to use for detected bean classes. Note that this will override any custom "scopedProxyMode" setting.

      The default is an `AnnotationScopeMetadataResolver`.

      另请参阅:
        `setScopedProxyMode(org.springframework.context.annotation.ScopedProxyMode)`

    - #### setScopedProxyMode

          public void setScopedProxyMode(ScopedProxyMode scopedProxyMode)

      Specify the proxy behavior for non-singleton scoped beans. Note that this will override any custom "scopeMetadataResolver" setting.

      The default is `ScopedProxyMode.NO`.

      另请参阅:
        `setScopeMetadataResolver(org.springframework.context.annotation.ScopeMetadataResolver)`

    - #### setIncludeAnnotationConfig

          public void setIncludeAnnotationConfig(boolean includeAnnotationConfig)

      Specify whether to register annotation config post-processors.

      The default is to register the post-processors. Turn this off to be able to ignore the annotations or to process them differently.

    - #### scan

          public int scan(String... basePackages)

      方法实现说明:真正的扫描我们@MapperScan的backpackage指定的路径的 Mapper包
      参数:
        `basePackages` - 包的路径

    - #### doScan

          protected Set<org.springframework.beans.factory.config.BeanDefinitionHolder> doScan(String... basePackages)

      Perform a scan within the specified base packages, returning the registered bean definitions.

      This method does not register an annotation config processor but rather leaves this up to the caller.

      参数:
        `basePackages` - the packages to check for annotated classes
      返回:
        set of beans registered if any for tooling registration purposes (never `null`)

    - #### postProcessBeanDefinition

          protected void postProcessBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition beanDefinition,
                                                   String beanName)

      Apply further settings to the given bean definition, beyond the contents retrieved from scanning the component class.
      参数:
        `beanDefinition` - the scanned bean definition
        `beanName` - the generated bean name for the given bean

    - #### registerBeanDefinition

          protected void registerBeanDefinition(org.springframework.beans.factory.config.BeanDefinitionHolder definitionHolder,
                                                org.springframework.beans.factory.support.BeanDefinitionRegistry registry)

      Register the specified bean with the given registry.

      Can be overridden in subclasses, e.g. to adapt the registration process or to register further bean definitions for each scanned bean.

      参数:
        `definitionHolder` - the bean definition plus bean name for the bean
        `registry` - the BeanDefinitionRegistry to register the bean with

    - #### checkCandidate

          protected boolean checkCandidate(String beanName,
                                           org.springframework.beans.factory.config.BeanDefinition beanDefinition)
                                    throws IllegalStateException

      Check the given candidate's bean name, determining whether the corresponding bean definition needs to be registered or conflicts with an existing definition.
      参数:
        `beanName` - the suggested name for the bean
        `beanDefinition` - the corresponding bean definition
      返回:
        `true` if the bean can be registered as-is; `false` if it should be skipped because there is an existing, compatible bean definition for the specified name
      抛出:
        `ConflictingBeanDefinitionException` - if an existing, incompatible bean definition has been found for the specified name
        `IllegalStateException`

    - #### isCompatible

          protected boolean isCompatible(org.springframework.beans.factory.config.BeanDefinition newDefinition,
                                         org.springframework.beans.factory.config.BeanDefinition existingDefinition)

      Determine whether the given new bean definition is compatible with the given existing bean definition.

      The default implementation considers them as compatible when the existing bean definition comes from the same source or from a non-scanning source.

      参数:
        `newDefinition` - the new bean definition, originated from scanning
        `existingDefinition` - the existing bean definition, potentially an explicitly defined one or a previously generated one from scanning
      返回:
        whether the definitions are considered as compatible, with the new definition to be skipped in favor of the existing definition