org.springframework.beans.factory

## 接口 BeanFactory

- 所有已知子接口:
    AutowireCapableBeanFactory, ConfigurableBeanFactory, ConfigurableListableBeanFactory, HierarchicalBeanFactory, ListableBeanFactory
  所有已知实现类:
    AbstractAutowireCapableBeanFactory, AbstractBeanFactory, DefaultListableBeanFactory, StaticListableBeanFactory, XmlBeanFactory

      public interface BeanFactory

  The root interface for accessing a Spring bean container. This is the basic client view of a bean container; further interfaces such as `ListableBeanFactory` and `ConfigurableBeanFactory` are available for specific purposes.

  This interface is implemented by objects that hold a number of bean definitions, each uniquely identified by a String name. Depending on the bean definition, the factory will return either an independent instance of a contained object (the Prototype design pattern), or a single shared instance (a superior alternative to the Singleton design pattern, in which the instance is a singleton in the scope of the factory). Which type of instance will be returned depends on the bean factory configuration: the API is the same. Since Spring 2.0, further scopes are available depending on the concrete application context (e.g. "request" and "session" scopes in a web environment).

  The point of this approach is that the BeanFactory is a central registry of application components, and centralizes configuration of application components (no more do individual objects need to read properties files, for example). See chapters 4 and 11 of "Expert One-on-One J2EE Design and Development" for a discussion of the benefits of this approach.

  Note that it is generally better to rely on Dependency Injection ("push" configuration) to configure application objects through setters or constructors, rather than use any form of "pull" configuration like a BeanFactory lookup. Spring's Dependency Injection functionality is implemented using this BeanFactory interface and its subinterfaces.

  Normally a BeanFactory will load bean definitions stored in a configuration source (such as an XML document), and use the `org.springframework.beans` package to configure the beans. However, an implementation could simply return Java objects it creates as necessary directly in Java code. There are no constraints on how the definitions could be stored: LDAP, RDBMS, XML, properties file, etc. Implementations are encouraged to support references amongst beans (Dependency Injection).

  In contrast to the methods in `ListableBeanFactory`, all of the operations in this interface will also check parent factories if this is a `HierarchicalBeanFactory`. If a bean is not found in this factory instance, the immediate parent factory will be asked. Beans in this factory instance are supposed to override beans of the same name in any parent factory.

  Bean factory implementations should support the standard bean lifecycle interfaces as far as possible. The full set of initialization methods and their standard order is:

  1. BeanNameAware's `setBeanName`
  2. BeanClassLoaderAware's `setBeanClassLoader`
  3. BeanFactoryAware's `setBeanFactory`
  4. EnvironmentAware's `setEnvironment`
  5. EmbeddedValueResolverAware's `setEmbeddedValueResolver`
  6. ResourceLoaderAware's `setResourceLoader` (only applicable when running in an application context)
  7. ApplicationEventPublisherAware's `setApplicationEventPublisher` (only applicable when running in an application context)
  8. MessageSourceAware's `setMessageSource` (only applicable when running in an application context)
  9. ApplicationContextAware's `setApplicationContext` (only applicable when running in an application context)
  10. ServletContextAware's `setServletContext` (only applicable when running in a web application context)
  11. `postProcessBeforeInitialization` methods of BeanPostProcessors
  12. InitializingBean's `afterPropertiesSet`
  13. a custom init-method definition
  14. `postProcessAfterInitialization` methods of BeanPostProcessors

  On shutdown of a bean factory, the following lifecycle methods apply:

  1. `postProcessBeforeDestruction` methods of DestructionAwareBeanPostProcessors
  2. DisposableBean's `destroy`
  3. a custom destroy-method definition
  从以下版本开始:
    13 April 2001 IOCbean工厂的顶级接口 定义一系列对bean的操作
  作者:
    Rod Johnson, Juergen Hoeller, Chris Beams

- - ### 字段概要

    字段
    限定符和类型 | 字段和说明
    --- | ---
    `static String` | `FACTORY_BEAN_PREFIX` 工厂Bean的前缀 Used to dereference a `FactoryBean` instance and distinguish it from beans created by the FactoryBean.

  - ### 方法概要

    所有方法 实例方法 抽象方法
    限定符和类型 | 方法和说明
    --- | ---
    `boolean` | `containsBean(String name)` 判断我们的容器中是否包含了当前的bean对象
    `String[]` | `getAliases(String name)` 获取bean的所有的别名 Will ask the parent factory if the bean cannot be found in this factory instance.
    `<T> T` | `getBean(Class<T> requiredType)` 通过指定的bean的类型去容器中获取对象 若容器中有多个想同类型的bean 我们通过ctx.getBean(beanType.class) 就会抛出异常
    `<T> T` | `getBean(Class<T> requiredType, Object... args)` 获取bean实例
    `Object` | `getBean(String name)` 通过bean 去容器中获取一个bean对象
    `<T> T` | `getBean(String name, Class<T> requiredType)` 通过bean 去容器中获取一个bean对象
    `Object` | `getBean(String name, Object... args)` 通过bean 去容器中获取一个bean对象
    `Class<?>` | `getType(String name)` 通过beanName获取对应bean的class类型
    `boolean` | `isPrototype(String name)` 是不是原型的
    `boolean` | `isSingleton(String name)` 判断当前的bean是不是单例的
    `boolean` | `isTypeMatch(String name, Class<?> typeToMatch)` 是不是匹配的类型
    `boolean` | `isTypeMatch(String name, org.springframework.core.ResolvableType typeToMatch)` 是不是匹配的类型 Will ask the parent factory if the bean cannot be found in this factory instance.

- - ### 字段详细资料

    - #### FACTORY_BEAN_PREFIX

          static final String FACTORY_BEAN_PREFIX

      工厂Bean的前缀 Used to dereference a `FactoryBean` instance and distinguish it from beans created by the FactoryBean. For example, if the bean named `myJndiObject` is a FactoryBean, getting `&myJndiObject` will return the factory, not the instance returned by the factory.
      另请参阅:
        常量字段值

  - ### 方法详细资料

    - #### getBean

          Object getBean(String name)
                  throws BeansException

      通过bean 去容器中获取一个bean对象
      参数:
        `name` - 名称(有可能是bean的真正的名称 也有可能是工厂bean的名称 也有可能是bean的别名)
      返回:
        返回bean实例
      抛出:
        `NoSuchBeanDefinitionException` - 抛出的异常，容器中没有该bean的定义
        `BeansException` - if the bean could not be obtained

    - #### getBean

          <T> T getBean(String name,
                        @Nullable
                        Class<T> requiredType)
                 throws BeansException

      通过bean 去容器中获取一个bean对象
      参数:
        `name` - 名称(有可能是bean的真正的名称 也有可能是工厂bean的名称 也有可能是bean的别名)
        `requiredType` - 去容器中获取Bean的class类型 可以是实现的接口和父类
      返回:
        返回Bean对应的实例
      抛出:
        `NoSuchBeanDefinitionException` - 抛出的异常，容器中没有该bean的定义
        `BeanNotOfRequiredTypeException` - 容器中没有指定class类型的bean
        `BeansException` - bean还没有被创建

    - #### getBean

          Object getBean(String name,
                         Object... args)
                  throws BeansException

      通过bean 去容器中获取一个bean对象
      参数:
        `name` - 名称(有可能是bean的真正的名称 也有可能是工厂bean的名称 也有可能是bean的别名)
        `args` - 这个是用于指定传入获取bean的构造函数的参数,通过传入该参数，那么就可以明确的知道去调用哪个构造函数是实例化对象
      返回:
        返回Bean对应的实例
      抛出:
        `NoSuchBeanDefinitionException` - 抛出的异常，容器中没有该bean的定义
        `BeanDefinitionStoreException` - 主要用于多例模式下的构造器创建？？？？暂时没理解
        `BeansException` - bean还没有被创建
      从以下版本开始:
        2.5

    - #### getBean

          <T> T getBean(Class<T> requiredType)
                 throws BeansException

      通过指定的bean的类型去容器中获取对象 若容器中有多个想同类型的bean 我们通过ctx.getBean(beanType.class) 就会抛出异常
      参数:
        `requiredType` - class对象需要的类型
      返回:
        返回的bean对象
      抛出:
        `NoSuchBeanDefinitionException` - 没有对应class的bean定义
        `NoUniqueBeanDefinitionException` - 找到多个匹配的
        `BeansException` - 创建bean的实例
      从以下版本开始:
        3.0
      另请参阅:
        `ListableBeanFactory`

    - #### getBean

          <T> T getBean(Class<T> requiredType,
                        Object... args)
                 throws BeansException

      获取bean实例
      参数:
        `requiredType` - bean的类型
        `args` - 构造函数的参数的类型，在实例化的过程中不需要去推断构造函数了
      返回:
        bean实例
      抛出:
        `NoSuchBeanDefinitionException` - if there is no such bean definition
        `BeanDefinitionStoreException` - if arguments have been given but the affected bean isn't a prototype
        `BeansException` - if the bean could not be created
      从以下版本开始:
        4.1

    - #### containsBean

          boolean containsBean(String name)

      判断我们的容器中是否包含了当前的bean对象
      参数:
        `name` - the name of the bean to query
      返回:
        whether a bean with the given name is present

    - #### isSingleton

          boolean isSingleton(String name)
                       throws NoSuchBeanDefinitionException

      判断当前的bean是不是单例的
      参数:
        `name` - the name of the bean to query
      返回:
        whether this bean corresponds to a singleton instance
      抛出:
        `NoSuchBeanDefinitionException` - if there is no bean with the given name
      另请参阅:
        `getBean(java.lang.String)`, `isPrototype(java.lang.String)`

    - #### isPrototype

          boolean isPrototype(String name)
                       throws NoSuchBeanDefinitionException

      是不是原型的
      参数:
        `name` - the name of the bean to query
      返回:
        whether this bean will always deliver independent instances
      抛出:
        `NoSuchBeanDefinitionException` - if there is no bean with the given name
      从以下版本开始:
        2.0.3
      另请参阅:
        `getBean(java.lang.String)`, `isSingleton(java.lang.String)`

    - #### isTypeMatch

          boolean isTypeMatch(String name,
                              org.springframework.core.ResolvableType typeToMatch)
                       throws NoSuchBeanDefinitionException

      是不是匹配的类型 Will ask the parent factory if the bean cannot be found in this factory instance.
      参数:
        `name` - the name of the bean to query
        `typeToMatch` - the type to match against (as a `ResolvableType`)
      返回:
        `true` if the bean type matches, `false` if it doesn't match or cannot be determined yet
      抛出:
        `NoSuchBeanDefinitionException` - if there is no bean with the given name
      从以下版本开始:
        4.2
      另请参阅:
        `getBean(java.lang.String)`, `getType(java.lang.String)`

    - #### isTypeMatch

          boolean isTypeMatch(String name,
                              @Nullable
                              Class<?> typeToMatch)
                       throws NoSuchBeanDefinitionException

      是不是匹配的类型
      参数:
        `name` - the name of the bean to query
        `typeToMatch` - the type to match against (as a `Class`)
      返回:
        `true` if the bean type matches, `false` if it doesn't match or cannot be determined yet
      抛出:
        `NoSuchBeanDefinitionException` - if there is no bean with the given name
      从以下版本开始:
        2.0.1
      另请参阅:
        `getBean(java.lang.String)`, `getType(java.lang.String)`

    - #### getType

          @Nullable
          Class<?> getType(String name)
                               throws NoSuchBeanDefinitionException

      通过beanName获取对应bean的class类型
      参数:
        `name` - the name of the bean to query
      返回:
        the type of the bean, or `null` if not determinable
      抛出:
        `NoSuchBeanDefinitionException` - if there is no bean with the given name
      从以下版本开始:
        1.1.2
      另请参阅:
        `getBean(java.lang.String)`, `isTypeMatch(java.lang.String, org.springframework.core.ResolvableType)`

    - #### getAliases

          String[] getAliases(String name)

      获取bean的所有的别名

      Will ask the parent factory if the bean cannot be found in this factory instance.

      参数:
        `name` - the bean name to check for aliases
      返回:
        the aliases, or an empty array if none
      另请参阅:
        `getBean(java.lang.String)`