org.springframework.beans.factory

## 类 BeanFactoryUtils

- java.lang.Object
- - org.springframework.beans.factory.BeanFactoryUtils

-     public abstract class BeanFactoryUtils
      extends Object

  Convenience methods operating on bean factories, in particular on the `ListableBeanFactory` interface.

  Returns bean counts, bean names or bean instances, taking into account the nesting hierarchy of a bean factory (which the methods defined on the ListableBeanFactory interface don't, in contrast to the methods defined on the BeanFactory interface).

  从以下版本开始:
    04.07.2003
  作者:
    Rod Johnson, Juergen Hoeller, Chris Beams

- - ### 字段概要

    字段
    限定符和类型 | 字段和说明
    --- | ---
    `static String` | `GENERATED_BEAN_NAME_SEPARATOR` Separator for generated bean names.

  - ### 构造器概要

    构造器
    构造器和说明
    `BeanFactoryUtils()`

  - ### 方法概要

    所有方法 静态方法 具体方法
    限定符和类型 | 方法和说明
    --- | ---
    `static String[]` | `beanNamesForAnnotationIncludingAncestors(ListableBeanFactory lbf, Class<? extends Annotation> annotationType)` Get all bean names whose `Class` has the supplied `Annotation` type, including those defined in ancestor factories, without creating any bean instances yet.
    `static String[]` | `beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, Class<?> type)` Get all bean names for the given type, including those defined in ancestor factories.
    `static String[]` | `beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, Class<?> type, boolean includeNonSingletons, boolean allowEagerInit)` Get all bean names for the given type, including those defined in ancestor factories.
    `static String[]` | `beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf, org.springframework.core.ResolvableType type)` Get all bean names for the given type, including those defined in ancestor factories.
    `static String[]` | `beanNamesIncludingAncestors(ListableBeanFactory lbf)` Return all bean names in the factory, including ancestor factories.
    `static <T> T` | `beanOfType(ListableBeanFactory lbf, Class<T> type)` Return a single bean of the given type or subtypes, not looking in ancestor factories.
    `static <T> T` | `beanOfType(ListableBeanFactory lbf, Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)` Return a single bean of the given type or subtypes, not looking in ancestor factories.
    `static <T> T` | `beanOfTypeIncludingAncestors(ListableBeanFactory lbf, Class<T> type)` Return a single bean of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory.
    `static <T> T` | `beanOfTypeIncludingAncestors(ListableBeanFactory lbf, Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)` Return a single bean of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory.
    `static <T> Map<String,T>` | `beansOfTypeIncludingAncestors(ListableBeanFactory lbf, Class<T> type)` Return all beans of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory.
    `static <T> Map<String,T>` | `beansOfTypeIncludingAncestors(ListableBeanFactory lbf, Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)` Return all beans of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory.
    `static int` | `countBeansIncludingAncestors(ListableBeanFactory lbf)` Count all beans in any hierarchy in which this factory participates.
    `static boolean` | `isFactoryDereference(String name)` Return whether the given name is a factory dereference (beginning with the factory dereference prefix).
    `static boolean` | `isGeneratedBeanName(String name)` Return whether the given name is a bean name which has been generated by the default naming strategy (containing a "#..." part).
    `static String` | `originalBeanName(String name)` Extract the "raw" bean name from the given (potentially generated) bean name, excluding any "#..." suffixes which might have been added for uniqueness.
    `static String` | `transformedBeanName(String name)` 去除工厂Bean的前缀(&)

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

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

- - ### 字段详细资料

    - #### GENERATED_BEAN_NAME_SEPARATOR

          public static final String GENERATED_BEAN_NAME_SEPARATOR

      Separator for generated bean names. If a class name or parent name is not unique, "#1", "#2" etc will be appended, until the name becomes unique.
      另请参阅:
        常量字段值

  - ### 构造器详细资料

    - #### BeanFactoryUtils

          public BeanFactoryUtils()

  - ### 方法详细资料

    - #### isFactoryDereference

          public static boolean isFactoryDereference(@Nullable
                                                     String name)

      Return whether the given name is a factory dereference (beginning with the factory dereference prefix).
      参数:
        `name` - the name of the bean
      返回:
        whether the given name is a factory dereference
      另请参阅:
        `BeanFactory.FACTORY_BEAN_PREFIX`

    - #### transformedBeanName

          public static String transformedBeanName(String name)

      去除工厂Bean的前缀(&)
      参数:
        `name` - bean的名称
      返回:
        转换之后的名称
      另请参阅:
        `BeanFactory.FACTORY_BEAN_PREFIX`

    - #### isGeneratedBeanName

          public static boolean isGeneratedBeanName(@Nullable
                                                    String name)

      Return whether the given name is a bean name which has been generated by the default naming strategy (containing a "#..." part).
      参数:
        `name` - the name of the bean
      返回:
        whether the given name is a generated bean name
      另请参阅:
        `GENERATED_BEAN_NAME_SEPARATOR`, `BeanDefinitionReaderUtils.generateBeanName(org.springframework.beans.factory.config.BeanDefinition, org.springframework.beans.factory.support.BeanDefinitionRegistry)`, `DefaultBeanNameGenerator`

    - #### originalBeanName

          public static String originalBeanName(String name)

      Extract the "raw" bean name from the given (potentially generated) bean name, excluding any "#..." suffixes which might have been added for uniqueness.
      参数:
        `name` - the potentially generated bean name
      返回:
        the raw bean name
      另请参阅:
        `GENERATED_BEAN_NAME_SEPARATOR`

    - #### countBeansIncludingAncestors

          public static int countBeansIncludingAncestors(ListableBeanFactory lbf)

      Count all beans in any hierarchy in which this factory participates. Includes counts of ancestor bean factories.

      Beans that are "overridden" (specified in a descendant factory with the same name) are only counted once.

      参数:
        `lbf` - the bean factory
      返回:
        count of beans including those defined in ancestor factories
      另请参阅:
        `beanNamesIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory)`

    - #### beanNamesIncludingAncestors

          public static String[] beanNamesIncludingAncestors(ListableBeanFactory lbf)

      Return all bean names in the factory, including ancestor factories.
      参数:
        `lbf` - the bean factory
      返回:
        the array of matching bean names, or an empty array if none
      另请参阅:
        `beanNamesForTypeIncludingAncestors(org.springframework.beans.factory.ListableBeanFactory, org.springframework.core.ResolvableType)`

    - #### beanNamesForTypeIncludingAncestors

          public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
                                                                    org.springframework.core.ResolvableType type)

      Get all bean names for the given type, including those defined in ancestor factories. Will return unique names in case of overridden bean definitions.

      Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

      This version of `beanNamesForTypeIncludingAncestors` automatically includes prototypes and FactoryBeans.

      参数:
        `lbf` - the bean factory
        `type` - the type that beans must match (as a `ResolvableType`)
      返回:
        the array of matching bean names, or an empty array if none
      从以下版本开始:
        4.2
      另请参阅:
        `ListableBeanFactory.getBeanNamesForType(ResolvableType)`

    - #### beanNamesForTypeIncludingAncestors

          public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
                                                                    Class<?> type)

      Get all bean names for the given type, including those defined in ancestor factories. Will return unique names in case of overridden bean definitions.

      Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

      This version of `beanNamesForTypeIncludingAncestors` automatically includes prototypes and FactoryBeans.

      参数:
        `lbf` - the bean factory
        `type` - the type that beans must match (as a `Class`)
      返回:
        the array of matching bean names, or an empty array if none
      另请参阅:
        `ListableBeanFactory.getBeanNamesForType(Class)`

    - #### beanNamesForTypeIncludingAncestors

          public static String[] beanNamesForTypeIncludingAncestors(ListableBeanFactory lbf,
                                                                    Class<?> type,
                                                                    boolean includeNonSingletons,
                                                                    boolean allowEagerInit)

      Get all bean names for the given type, including those defined in ancestor factories. Will return unique names in case of overridden bean definitions.

      Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).

      参数:
        `lbf` - the bean factory
        `includeNonSingletons` - whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
        `allowEagerInit` - whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
        `type` - the type that beans must match
      返回:
        the array of matching bean names, or an empty array if none
      另请参阅:
        `ListableBeanFactory.getBeanNamesForType(Class, boolean, boolean)`

    - #### beanNamesForAnnotationIncludingAncestors

          public static String[] beanNamesForAnnotationIncludingAncestors(ListableBeanFactory lbf,
                                                                          Class<? extends Annotation> annotationType)

      Get all bean names whose `Class` has the supplied `Annotation` type, including those defined in ancestor factories, without creating any bean instances yet. Will return unique names in case of overridden bean definitions.
      参数:
        `lbf` - the bean factory
        `annotationType` - the type of annotation to look for
      返回:
        the array of matching bean names, or an empty array if none
      从以下版本开始:
        5.0
      另请参阅:
        `ListableBeanFactory.getBeanNamesForAnnotation(Class)`

    - #### beansOfTypeIncludingAncestors

          public static <T> Map<String,T> beansOfTypeIncludingAncestors(ListableBeanFactory lbf,
                                                                        Class<T> type)
                                                                 throws BeansException

      Return all beans of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory. The returned Map will only contain beans of this type.

      Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

      Note: Beans of the same name will take precedence at the 'lowest' factory level, i.e. such beans will be returned from the lowest factory that they are being found in, hiding corresponding beans in ancestor factories. This feature allows for 'replacing' beans by explicitly choosing the same bean name in a child factory; the bean in the ancestor factory won't be visible then, not even for by-type lookups.

      参数:
        `lbf` - the bean factory
        `type` - type of bean to match
      返回:
        the Map of matching bean instances, or an empty Map if none
      抛出:
        `BeansException` - if a bean could not be created
      另请参阅:
        `ListableBeanFactory.getBeansOfType(Class)`

    - #### beansOfTypeIncludingAncestors

          public static <T> Map<String,T> beansOfTypeIncludingAncestors(ListableBeanFactory lbf,
                                                                        Class<T> type,
                                                                        boolean includeNonSingletons,
                                                                        boolean allowEagerInit)
                                                                 throws BeansException

      Return all beans of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory. The returned Map will only contain beans of this type.

      Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).

      Note: Beans of the same name will take precedence at the 'lowest' factory level, i.e. such beans will be returned from the lowest factory that they are being found in, hiding corresponding beans in ancestor factories. This feature allows for 'replacing' beans by explicitly choosing the same bean name in a child factory; the bean in the ancestor factory won't be visible then, not even for by-type lookups.

      参数:
        `lbf` - the bean factory
        `type` - type of bean to match
        `includeNonSingletons` - whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
        `allowEagerInit` - whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
      返回:
        the Map of matching bean instances, or an empty Map if none
      抛出:
        `BeansException` - if a bean could not be created
      另请参阅:
        `ListableBeanFactory.getBeansOfType(Class, boolean, boolean)`

    - #### beanOfTypeIncludingAncestors

          public static <T> T beanOfTypeIncludingAncestors(ListableBeanFactory lbf,
                                                           Class<T> type)
                                                    throws BeansException

      Return a single bean of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory. Useful convenience method when we expect a single bean and don't care about the bean name.

      Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

      This version of `beanOfTypeIncludingAncestors` automatically includes prototypes and FactoryBeans.

      Note: Beans of the same name will take precedence at the 'lowest' factory level, i.e. such beans will be returned from the lowest factory that they are being found in, hiding corresponding beans in ancestor factories. This feature allows for 'replacing' beans by explicitly choosing the same bean name in a child factory; the bean in the ancestor factory won't be visible then, not even for by-type lookups.

      参数:
        `lbf` - the bean factory
        `type` - type of bean to match
      返回:
        the matching bean instance
      抛出:
        `NoSuchBeanDefinitionException` - if no bean of the given type was found
        `NoUniqueBeanDefinitionException` - if more than one bean of the given type was found
        `BeansException` - if the bean could not be created
      另请参阅:
        `beansOfTypeIncludingAncestors(ListableBeanFactory, Class)`

    - #### beanOfTypeIncludingAncestors

          public static <T> T beanOfTypeIncludingAncestors(ListableBeanFactory lbf,
                                                           Class<T> type,
                                                           boolean includeNonSingletons,
                                                           boolean allowEagerInit)
                                                    throws BeansException

      Return a single bean of the given type or subtypes, also picking up beans defined in ancestor bean factories if the current bean factory is a HierarchicalBeanFactory. Useful convenience method when we expect a single bean and don't care about the bean name.

      Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).

      Note: Beans of the same name will take precedence at the 'lowest' factory level, i.e. such beans will be returned from the lowest factory that they are being found in, hiding corresponding beans in ancestor factories. This feature allows for 'replacing' beans by explicitly choosing the same bean name in a child factory; the bean in the ancestor factory won't be visible then, not even for by-type lookups.

      参数:
        `lbf` - the bean factory
        `type` - type of bean to match
        `includeNonSingletons` - whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
        `allowEagerInit` - whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
      返回:
        the matching bean instance
      抛出:
        `NoSuchBeanDefinitionException` - if no bean of the given type was found
        `NoUniqueBeanDefinitionException` - if more than one bean of the given type was found
        `BeansException` - if the bean could not be created
      另请参阅:
        `beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)`

    - #### beanOfType

          public static <T> T beanOfType(ListableBeanFactory lbf,
                                         Class<T> type)
                                  throws BeansException

      Return a single bean of the given type or subtypes, not looking in ancestor factories. Useful convenience method when we expect a single bean and don't care about the bean name.

      Does consider objects created by FactoryBeans, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type.

      This version of `beanOfType` automatically includes prototypes and FactoryBeans.

      参数:
        `lbf` - the bean factory
        `type` - type of bean to match
      返回:
        the matching bean instance
      抛出:
        `NoSuchBeanDefinitionException` - if no bean of the given type was found
        `NoUniqueBeanDefinitionException` - if more than one bean of the given type was found
        `BeansException` - if the bean could not be created
      另请参阅:
        `ListableBeanFactory.getBeansOfType(Class)`

    - #### beanOfType

          public static <T> T beanOfType(ListableBeanFactory lbf,
                                         Class<T> type,
                                         boolean includeNonSingletons,
                                         boolean allowEagerInit)
                                  throws BeansException

      Return a single bean of the given type or subtypes, not looking in ancestor factories. Useful convenience method when we expect a single bean and don't care about the bean name.

      Does consider objects created by FactoryBeans if the "allowEagerInit" flag is set, which means that FactoryBeans will get initialized. If the object created by the FactoryBean doesn't match, the raw FactoryBean itself will be matched against the type. If "allowEagerInit" is not set, only raw FactoryBeans will be checked (which doesn't require initialization of each FactoryBean).

      参数:
        `lbf` - the bean factory
        `type` - type of bean to match
        `includeNonSingletons` - whether to include prototype or scoped beans too or just singletons (also applies to FactoryBeans)
        `allowEagerInit` - whether to initialize lazy-init singletons and objects created by FactoryBeans (or by factory methods with a "factory-bean" reference) for the type check. Note that FactoryBeans need to be eagerly initialized to determine their type: So be aware that passing in "true" for this flag will initialize FactoryBeans and "factory-bean" references.
      返回:
        the matching bean instance
      抛出:
        `NoSuchBeanDefinitionException` - if no bean of the given type was found
        `NoUniqueBeanDefinitionException` - if more than one bean of the given type was found
        `BeansException` - if the bean could not be created
      另请参阅:
        `ListableBeanFactory.getBeansOfType(Class, boolean, boolean)`