/**
* Used to dereference a {@link FactoryBean} instance and distinguish it from
* beans <i>created</i> by the FactoryBean. For example, if the bean named
* {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject}
* will return the factory, not the instance returned by the factory.
*
*/
String FACTORY_BEAN_PREFIX = "&";
这个符号用来间接引用factorybean的实例,用来区分factorybean创建的对象。例如:如果myJndiObject是个factoryBean,那么get &myJndiObject将获得这个工厂实例,而不是这个工厂返回(创造)的bean。
/**
* Return an instance, which may be shared or independent, of the specified bean.
* <p>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.
* <p>Translates aliases back to the corresponding canonical bean name.
* Will ask the parent factory if the bean cannot be found in this factory instance.
* @param name the name of the bean to retrieve
* @return an instance of the bean
* @throws NoSuchBeanDefinitionException if there is no bean with the specified name
* @throws BeansException if the bean could not be obtained
*/
Object getBean(String name) throws BeansException;
返回这个bean一个共享或者独立的实例。这个方法允许springfactory替换单例或者多例模式。调用可以持有在单例模式下返回的对象。将别名转换回对应的规范名称。如果这个工厂不能创建,将会查找他的父工厂。
/**
* Return an instance, which may be shared or independent, of the specified bean.
* <p>Behaves the same as {@link #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 {@link #getBean(String)}.
* <p>Translates aliases back to the corresponding canonical bean name.
* Will ask the parent factory if the bean cannot be found in this factory instance.
* @param name the name of the bean to retrieve
* @param requiredType type the bean must match; can be an interface or superclass
* @return an instance of the bean
* @throws NoSuchBeanDefinitionException if there is no such bean definition
* @throws BeanNotOfRequiredTypeException if the bean is not of the required type
* @throws BeansException if the bean could not be created
*/
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
针对特定bean返回一个实例,可能是共享的或者独立的。和getbean(name)功能类似。但是如果这个bean 不是要求的类型,通过抛出BeanNotOfRequiredTypeException 异常提供一种类型安全的校验。这意味着像可能在getbean(name)后强转结果时发生的ClassCastException不会发生。将别名转换为对应的规范bean名称。
/**
* Return an instance, which may be shared or independent, of the specified bean.
* <p>Allows for specifying explicit constructor arguments / factory method arguments,
* overriding the specified default arguments (if any) in the bean definition.
* @param name the name of the bean to retrieve
* @param 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)
* @return an instance of the bean
* @throws NoSuchBeanDefinitionException if there is no such bean definition
* @throws BeanDefinitionStoreException if arguments have been given but
* the affected bean isn't a prototype
* @throws BeansException if the bean could not be created
* @since 2.5
*/
Object getBean(String name, Object... args) throws BeansException;
允许指定显示构造函数和工厂方法参数来bean定义中的默认参数(如果有)。
name:检索这个bean需要的名字
args:创造bean实例的用的显式参数(仅仅当创建新实例而不是获取已有的时候要用到)
/**
* Return the bean instance that uniquely matches the given object type, if any.
* <p>This method goes into {@link 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 {@link ListableBeanFactory} and/or {@link BeanFactoryUtils}.
* @param requiredType type the bean must match; can be an interface or superclass
* @return an instance of the single bean matching the required type
* @throws NoSuchBeanDefinitionException if no bean of the given type was found
* @throws NoUniqueBeanDefinitionException if more than one bean of the given type was found
* @throws BeansException if the bean could not be created
* @since 3.0
* @see ListableBeanFactory
*/
<T> T getBean(Class<T> requiredType) throws BeansException;
如果有的话,返回唯一匹配给定类型的bean实例。
这个方法通过ListableBeanFactory 按照类型查找,也可以基于给定类型的名字转化为正式的按名字查找。更多针对bean集合的检索操作,可以查看ListableBeanFactory和BeanFactoryUtils
/**
* Return an provider for the specified bean, allowing for lazy on-demand retrieval
* of instances, including availability and uniqueness options.
* @param requiredType type the bean must match; can be an interface or superclass
* @return a corresponding provider handle
* @since 5.1
* @see #getBeanProvider(ResolvableType)
*/
<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
为指定bean提供一个provider,允许延迟按需检索,包含可用性和唯一性选项。
/**
* Return an provider for the specified bean, allowing for lazy on-demand retrieval
* of instances, including availability and uniqueness options.
* @param 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 {@link ObjectProvider#orderedStream()} or its lazy streaming/iteration options.
* @return a corresponding provider handle
* @since 5.1
* @see ObjectProvider#iterator()
* @see ObjectProvider#stream()
* @see ObjectProvider#orderedStream()
*/
<T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);
类型bean必须匹配;可以是泛型类型声明。请注意,与反射注入点相比,此处不支持集合类型。要以编程方式检索与特定类型匹配的bean列表,请在此处和随后将实际bean类型指定为参数
其他几个方法意思注释比较明了,这里就不再赘述了
思考:
- 这边可能会有人疑惑,这边的getbeanProvider是干嘛的?不是有getBean了么,为什么还要有getbeanProvider
- 上面部分方法里有参数ResolvableType ,这个是什么,和其他的区别在哪里?
这个问题留给我,也留给大家