接口 BeanFactory

    • 字段概要

      字段 
      修饰符和类型字段说明
      static StringFACTORY_BEAN_PREFIX
      Used to dereference a FactoryBean instance and distinguish it from beans created by the FactoryBean.
    • 方法概要

      所有方法 实例方法 抽象方法 
      修饰符和类型方法说明
      booleancontainsBean​(String name)
      Does this bean factory contain a bean definition or externally registered singleton instance with the given name?
      String[]getAliases​(String name)
      Return the aliases for the given bean name, if any.
      <T> TgetBean​(Class<T> requiredType)
      Return the bean instance that uniquely matches the given object type, if any.
      <T> TgetBean​(Class<T> requiredType, Object... args)
      Return an instance, which may be shared or independent, of the specified bean.
      ObjectgetBean​(String name)
      Return an instance, which may be shared or independent, of the specified bean.
      <T> TgetBean​(String name, Class<T> requiredType)
      Return an instance, which may be shared or independent, of the specified bean.
      ObjectgetBean​(String name, Object... args)
      Return an instance, which may be shared or independent, of the specified bean.
      <T> ObjectProvider<T>getBeanProvider​(Class<T> requiredType)
      Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.
      <T> ObjectProvider<T>getBeanProvider​(ResolvableType requiredType)
      Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.
      Class<?>getType​(String name)
      Determine the type of the bean with the given name.
      Class<?>getType​(String name, boolean allowFactoryBeanInit)
      Determine the type of the bean with the given name.
      booleanisPrototype​(String name)
      Is this bean a prototype?
      booleanisSingleton​(String name)
      Is this bean a shared singleton?
      booleanisTypeMatch​(String name, Class<?> typeToMatch)
      Check whether the bean with the given name matches the specified type.
      booleanisTypeMatch​(String name, ResolvableType typeToMatch)
      Check whether the bean with the given name matches the specified type.
    • 字段详细资料

      • FACTORY_BEAN_PREFIX

        static final String FACTORY_BEAN_PREFIX
        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
        Return an instance, which may be shared or independent, of the specified bean.

        This method allows a Spring BeanFactory to be used as a replacement for the Singleton or Prototype design pattern. Callers may retain references to returned objects in the case of Singleton beans.

        Translates aliases back to the corresponding canonical bean name.

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

        参数:
        name - the name of the bean to retrieve
        返回:
        an instance of the bean
        抛出:
        NoSuchBeanDefinitionException - if there is no bean with the specified name
        BeansException - if the bean could not be obtained
      • getBean

        <T> T getBean​(String name,
                      Class<T> requiredType)
               throws BeansException
        Return an instance, which may be shared or independent, of the specified bean.

        Behaves the same as getBean(String), but provides a measure of type safety by throwing a BeanNotOfRequiredTypeException if the bean is not of the required type. This means that ClassCastException can't be thrown on casting the result correctly, as can happen with getBean(String).

        Translates aliases back to the corresponding canonical bean name.

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

        参数:
        name - the name of the bean to retrieve
        requiredType - type the bean must match; can be an interface or superclass
        返回:
        an instance of the bean
        抛出:
        NoSuchBeanDefinitionException - if there is no such bean definition
        BeanNotOfRequiredTypeException - if the bean is not of the required type
        BeansException - if the bean could not be created
      • getBean

        Object getBean​(String name,
                       Object... args)
                throws BeansException
        Return an instance, which may be shared or independent, of the specified bean.

        Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition.

        参数:
        name - the name of the bean to retrieve
        args - arguments to use when creating a bean instance using explicit arguments (only applied when creating a new instance as opposed to retrieving an existing one)
        返回:
        an instance of the 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
        从以下版本开始:
        2.5
      • getBean

        <T> T getBean​(Class<T> requiredType,
                      Object... args)
               throws BeansException
        Return an instance, which may be shared or independent, of the specified bean.

        Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition.

        This method goes into ListableBeanFactory by-type lookup territory but may also be translated into a conventional by-name lookup based on the name of the given type. For more extensive retrieval operations across sets of beans, use ListableBeanFactory and/or BeanFactoryUtils.

        参数:
        requiredType - type the bean must match; can be an interface or superclass
        args - arguments to use when creating a bean instance using explicit arguments (only applied when creating a new instance as opposed to retrieving an existing one)
        返回:
        an instance of the 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
      • getBeanProvider

        <T> ObjectProvider<T> getBeanProvider​(Class<T> requiredType)
        Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.
        参数:
        requiredType - type the bean must match; can be an interface or superclass
        返回:
        a corresponding provider handle
        从以下版本开始:
        5.1
        另请参阅:
        getBeanProvider(ResolvableType)
      • getBeanProvider

        <T> ObjectProvider<T> getBeanProvider​(ResolvableType requiredType)
        Return a provider for the specified bean, allowing for lazy on-demand retrieval of instances, including availability and uniqueness options.
        参数:
        requiredType - type the bean must match; can be a generic type declaration. Note that collection types are not supported here, in contrast to reflective injection points. For programmatically retrieving a list of beans matching a specific type, specify the actual bean type as an argument here and subsequently use ObjectProvider.orderedStream() or its lazy streaming/iteration options.
        返回:
        a corresponding provider handle
        从以下版本开始:
        5.1
        另请参阅:
        ObjectProvider.iterator(), ObjectProvider.stream(), ObjectProvider.orderedStream()
      • containsBean

        boolean containsBean​(String name)
        Does this bean factory contain a bean definition or externally registered singleton instance with the given name?

        If the given name is an alias, it will be translated back to the corresponding canonical bean name.

        If this factory is hierarchical, will ask any parent factory if the bean cannot be found in this factory instance.

        If a bean definition or singleton instance matching the given name is found, this method will return true whether the named bean definition is concrete or abstract, lazy or eager, in scope or not. Therefore, note that a true return value from this method does not necessarily indicate that getBean(java.lang.String) will be able to obtain an instance for the same name.

        参数:
        name - the name of the bean to query
        返回:
        whether a bean with the given name is present
      • isTypeMatch

        boolean isTypeMatch​(String name,
                            ResolvableType typeToMatch)
                     throws NoSuchBeanDefinitionException
        Check whether the bean with the given name matches the specified type. More specifically, check whether a getBean(java.lang.String) call for the given name would return an object that is assignable to the specified target type.

        Translates aliases back to the corresponding canonical bean name.

        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,
                            Class<?> typeToMatch)
                     throws NoSuchBeanDefinitionException
        Check whether the bean with the given name matches the specified type. More specifically, check whether a getBean(java.lang.String) call for the given name would return an object that is assignable to the specified target type.

        Translates aliases back to the corresponding canonical bean name.

        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 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)
      • getAliases

        String[] getAliases​(String name)
        Return the aliases for the given bean name, if any.

        All of those aliases point to the same bean when used in a getBean(java.lang.String) call.

        If the given name is an alias, the corresponding original bean name and other aliases (if any) will be returned, with the original bean name being the first element in the array.

        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)