package org.springframework.beans.factory;import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;/*** The root interface for accessing a Spring bean container.* 用于访问Spring bean容器的根接口。 * <p>This is the basic client view of a bean container;* further interfaces such as {@link ListableBeanFactory} and* {@link org.springframework.beans.factory.config.ConfigurableBeanFactory}* are available for specific purposes.这是bean容器的基本客户端视图;其他接口,如ListableBeanFactory和org.springframework.beans.factory.config。ConfigurationBeanFactory可用于特定用途。** <p>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).这个接口是由包含许多bean定义的对象实现的,每个定义都由一个String名称唯一标识。根据bean定义,工厂将返回包含对象的独立实例(原型设计模式)或单个共享实例(Singleton设计模式的高级替代方案,其中实例是工厂范围内的单例)。返回哪种类型的实例取决于bean工厂配置:API是相同的。自Spring 2.0以来,根据具体的应用程序上下文(例如,web环境中的“请求”和“会话”作用域),可以使用更多的作用域。** <p>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.这种方法的要点是BeanFactory是应用程序组件的中心注册表,并集中应用程序组件配置(例如,单个对象不再需要读取属性文件)。请参阅“专家一对一J2EE设计和开发”的第4章和第11章,以讨论这种方法的好处。** <p>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.请注意,通常最好依靠依赖注入(“推”配置)通过setter或构造函数来配置应用程序对象,而不是使用任何形式的“拉”配置(如BeanFactory查找)。Spring的依赖注入功能是使用这个BeanFactory接口及其子接口实现的。** <p>Normally a BeanFactory will load bean definitions stored in a configuration* source (such as an XML document), and use the {@code 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).通常,BeanFactory将加载存储在配置源(如XML文档)中的bean定义,并使用org.springframework.beans包来配置bean。然而,实现可以简单地返回它在必要时直接在Java代码中创建的Java对象。定义的存储方式没有任何限制:LDAP、RDBMS、XML、属性文件等。鼓励实现支持bean之间的引用(依赖注入)。** <p>In contrast to the methods in {@link ListableBeanFactory}, all of the* operations in this interface will also check parent factories if this is a* {@link 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.*与ListableBeanFactory中的方法不同,如果这是HierarchicalBeanFactory,则此接口中的所有操作也将检查父工厂。如果在这个工厂实例中没有找到bean,则会询问直接的父工厂。这个工厂实例中的bean应该覆盖任何父工厂中相同名称的bean。* <p>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:Bean工厂实现应该尽可能支持标准的Bean生命周期接口。整套初始化方法及其标准顺序为:1.BeanNameAware's setBeanName
2.BeanClassLoaderAware's setBeanClassLoader
3.BeanFactoryAware's setBeanFactory
4.EnvironmentAware's setEnvironment
5.EmbeddedValueResolverAware's setEmbeddedValueResolver
6.ResourceLoaderAware's setResourceLoader (仅在应用程序上下文中运行时适用)
7.ApplicationEventPublisherAware's setApplicationEventPublisher (仅在应用程序上下文中运行时适用)
8.MessageSourceAware's setMessageSource (仅在应用程序上下文中运行时适用)
9.ApplicationContextAware's setApplicationContext (仅在应用程序上下文中运行时适用)
10.ServletContextAware's setServletContext (仅适用于在web应用程序上下文中运行时)
11.postProcessBeforeInitialization methods of BeanPostProcessors
12.InitializingBean's afterPropertiesSet
13.a custom init-method definition
14.postProcessAfterInitialization methods of BeanPostProcessors在idea中以下@see可以跳转到对应的接口和抽象类中* @author Rod Johnson* @author Juergen Hoeller* @author Chris Beams* @since 13 April 2001* @see BeanNameAware#setBeanName* @see BeanClassLoaderAware#setBeanClassLoader* @see BeanFactoryAware#setBeanFactory* @see org.springframework.context.EnvironmentAware#setEnvironment* @see org.springframework.context.EmbeddedValueResolverAware#setEmbeddedValueResolver* @see org.springframework.context.ResourceLoaderAware#setResourceLoader* @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher* @see org.springframework.context.MessageSourceAware#setMessageSource* @see org.springframework.context.ApplicationContextAware#setApplicationContext* @see org.springframework.web.context.ServletContextAware#setServletContext* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization* @see InitializingBean#afterPropertiesSet* @see org.springframework.beans.factory.support.RootBeanDefinition#getInitMethodName* @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization* @see org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor#postProcessBeforeDestruction* @see DisposableBean#destroy* @see org.springframework.beans.factory.support.RootBeanDefinition#getDestroyMethodName*/
public interface BeanFactory {/*** 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.*/
用于取消引用FactoryBean实例,并将其与FactoryBean创建的bean区分开来。例如,如果名为myJndiObject的bean是FactoryBean,那么获取&myJndiObject将返回工厂,而不是工厂返回的实例。String FACTORY_BEAN_PREFIX = "&";/*** 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.* <p>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*/返回指定bean的一个实例,该实例可以是共享的,也可以是独立的。
此方法允许使用Spring BeanFactory作为Singleton或Prototype设计模式的替代品。在Singleton bean的情况下,调用者可以保留对返回对象的引用。Object getBean(String name) throws BeansException;/*** 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.* <p>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*/返回指定bean的一个实例,该实例可以是共享的,也可以是独立的。
行为与getBean(String)相同,但如果bean不是必需的类型,则通过抛出BeanNotOfRequiredTypeException来提供类型安全性度量。这意味着ClassCastException不能像getBean(String)那样在正确地转换结果时抛出。<T> T getBean(String name, Class<T> requiredType) throws BeansException;/*** 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*/返回指定bean的一个实例,该实例可以是共享的,也可以是独立的。
允许指定显式构造函数参数/工厂方法参数,覆盖bean定义中指定的默认参数(如果有的话)。Object getBean(String name, Object... args) throws BeansException;/*** 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*/返回唯一匹配给定对象类型的bean实例(如果有的话)。
此方法进入ListableBeanFactory按类型查找区域,但也可以根据给定类型的名称转换为传统的按名称查找。对于跨bean集的更广泛的检索操作,请使用ListableBeanFactory和/或BeanFactoryUtils。<T> T getBean(Class<T> requiredType) throws BeansException;/*** 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.* <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* @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 4.1*/返回指定bean的一个实例,该实例可以是共享的,也可以是独立的。
允许指定显式构造函数参数/工厂方法参数,覆盖bean定义中指定的默认参数(如果有的话)。
此方法进入ListableBeanFactory按类型查找区域,但也可以根据给定类型的名称转换为传统的按名称查找。对于跨bean集的更广泛的检索操作,请使用ListableBeanFactory和/或BeanFactoryUtils。<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;/*** Return a provider for the specified bean, allowing for lazy on-demand retrieval* of instances, including availability and uniqueness options.* <p>For matching a generic type, consider {@link #getBeanProvider(ResolvableType)}.* @param requiredType type the bean must match; can be an interface or superclass* @return a corresponding provider handle* @since 5.1* @see #getBeanProvider(ResolvableType)*/返回指定bean的提供程序,允许延迟按需检索实例,包括可用性和唯一性选项。
要匹配泛型类型,请考虑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. This variant allows* for specifying a generic type to match, similar to reflective injection points* with generic type declarations in method/constructor parameters.* <p>Note that collections of beans 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.* <p>Also, generics matching is strict here, as per the Java assignment rules.* For lenient fallback matching with unchecked semantics (similar to the ´unchecked´* Java compiler warning), consider calling {@link #getBeanProvider(Class)} with the* raw type as a second step if no full generic match is* {@link ObjectProvider#getIfAvailable() available} with this variant.* @return a corresponding provider handle* @param requiredType type the bean must match; can be a generic type declaration* @since 5.1* @see ObjectProvider#iterator()* @see ObjectProvider#stream()* @see ObjectProvider#orderedStream()*/返回指定bean的提供程序,允许延迟按需检索实例,包括可用性和唯一性选项。此变体允许指定要匹配的泛型类型,类似于方法/构造函数参数中具有泛型类型声明的反射注入点。
请注意,与反射注入点相比,这里不支持bean的集合。要以编程方式检索与特定类型匹配的bean列表,请在此处指定实际bean类型作为参数,然后使用ObjectProvider.orderedStream()或其惰性流/迭代选项。
此外,根据Java赋值规则,泛型匹配在这里是严格的。对于具有未检查语义的宽松回退匹配(类似于“未检查”Java编译器警告),如果此变体没有完全的通用匹配,请考虑使用原始类型调用getBeanProvider(Class)作为第二步。<T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);/*** Does this bean factory contain a bean definition or externally registered singleton* instance with the given name?* <p>If the given name is an alias, it will be translated back to the corresponding* canonical bean name.* <p>If this factory is hierarchical, will ask any parent factory if the bean cannot* be found in this factory instance.* <p>If a bean definition or singleton instance matching the given name is found,* this method will return {@code true} whether the named bean definition is concrete* or abstract, lazy or eager, in scope or not. Therefore, note that a {@code true}* return value from this method does not necessarily indicate that {@link #getBean}* will be able to obtain an instance for the same name.* @param name the name of the bean to query* @return whether a bean with the given name is present*/这个bean工厂是否包含给定名称的bean定义或外部注册的singleton实例?
如果给定的名称是一个别名,它将被翻译回相应的规范bean名称。
如果这个工厂是分层的,将询问任何父工厂是否在这个工厂实例中找不到bean。
如果找到与给定名称匹配的bean定义或singleton实例,则无论命名的bean定义是具体的还是抽象的、懒惰的还是渴望的、在范围内的还是不在,此方法都将返回true。因此,请注意,此方法的真实返回值并不一定表示getBean能够获得相同名称的实例。boolean containsBean(String name);/*** Is this bean a shared singleton? That is, will {@link #getBean} always* return the same instance?* <p>Note: This method returning {@code false} does not clearly indicate* independent instances. It indicates non-singleton instances, which may correspond* to a scoped bean as well. Use the {@link #isPrototype} operation to explicitly* check for independent instances.* <p>Translates aliases back to the corresponding canonical bean name.* <p>Will ask the parent factory if the bean cannot be found in this factory instance.* @param name the name of the bean to query* @return whether this bean corresponds to a singleton instance* @throws NoSuchBeanDefinitionException if there is no bean with the given name* @see #getBean* @see #isPrototype*/这个bean是共享的singleton吗?也就是说,getBean是否总是返回相同的实例?
注意:这种返回false的方法并不能清楚地指示独立的实例。它指示非单例实例,这些实例也可能对应于作用域bean。使用isPrototype操作显式检查独立实例。
将别名翻译回相应的规范bean名称。
将询问父工厂是否在此工厂实例中找不到bean。boolean isSingleton(String name) throws NoSuchBeanDefinitionException;/*** Is this bean a prototype? That is, will {@link #getBean} always return* independent instances?* <p>Note: This method returning {@code false} does not clearly indicate* a singleton object. It indicates non-independent instances, which may correspond* to a scoped bean as well. Use the {@link #isSingleton} operation to explicitly* check for a shared singleton instance.* <p>Translates aliases back to the corresponding canonical bean name.* <p>Will ask the parent factory if the bean cannot be found in this factory instance.* @param name the name of the bean to query* @return whether this bean will always deliver independent instances* @throws NoSuchBeanDefinitionException if there is no bean with the given name* @since 2.0.3* @see #getBean* @see #isSingleton*/这个Bean是原型吗?也就是说,getBean总是会返回独立的实例吗?
注意:这个返回false的方法并不能清楚地指示一个singleton对象。它指示非独立实例,这些实例也可能对应于作用域bean。使用isSingleton操作显式检查共享的单例实例boolean isPrototype(String name) throws NoSuchBeanDefinitionException;/*** Check whether the bean with the given name matches the specified type.* More specifically, check whether a {@link #getBean} call for the given name* would return an object that is assignable to the specified target type.* <p>Translates aliases back to the corresponding canonical bean name.* <p>Will ask the parent factory if the bean cannot be found in this factory instance.* @param name the name of the bean to query* @param typeToMatch the type to match against (as a {@code ResolvableType})* @return {@code true} if the bean type matches,* {@code false} if it doesn't match or cannot be determined yet* @throws NoSuchBeanDefinitionException if there is no bean with the given name* @since 4.2* @see #getBean* @see #getType*/检查具有给定名称的bean是否与指定的类型匹配。更具体地说,检查给定名称的getBean调用是否会返回可分配给指定目标类型的对象。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 {@link #getBean} call for the given name* would return an object that is assignable to the specified target type.* <p>Translates aliases back to the corresponding canonical bean name.* <p>Will ask the parent factory if the bean cannot be found in this factory instance.* @param name the name of the bean to query* @param typeToMatch the type to match against (as a {@code Class})* @return {@code true} if the bean type matches,* {@code false} if it doesn't match or cannot be determined yet* @throws NoSuchBeanDefinitionException if there is no bean with the given name* @since 2.0.1* @see #getBean* @see #getType*/检查具有给定名称的bean是否与指定的类型匹配。更具体地说,检查给定名称的getBean调用是否会返回可分配给指定目标类型的对象。boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;/*** Determine the type of the bean with the given name. More specifically,* determine the type of object that {@link #getBean} would return for the given name.* <p>For a {@link FactoryBean}, return the type of object that the FactoryBean creates,* as exposed by {@link FactoryBean#getObjectType()}. This may lead to the initialization* of a previously uninitialized {@code FactoryBean} (see {@link #getType(String, boolean)}).* <p>Translates aliases back to the corresponding canonical bean name.* <p>Will ask the parent factory if the bean cannot be found in this factory instance.* @param name the name of the bean to query* @return the type of the bean, or {@code null} if not determinable* @throws NoSuchBeanDefinitionException if there is no bean with the given name* @since 1.1.2* @see #getBean* @see #isTypeMatch*/确定具有给定名称的bean的类型。更具体地说,确定getBean将为给定名称返回的对象类型。
对于FactoryBean,返回FactoryBean创建的对象类型,如FactoryBean.getObjectType()所示。这可能会导致初始化先前未初始化的FactoryBean(请参阅getType(String,boolean))。@NullableClass<?> getType(String name) throws NoSuchBeanDefinitionException;/*** Determine the type of the bean with the given name. More specifically,* determine the type of object that {@link #getBean} would return for the given name.* <p>For a {@link FactoryBean}, return the type of object that the FactoryBean creates,* as exposed by {@link FactoryBean#getObjectType()}. Depending on the* {@code allowFactoryBeanInit} flag, this may lead to the initialization of a previously* uninitialized {@code FactoryBean} if no early type information is available.* <p>Translates aliases back to the corresponding canonical bean name.* <p>Will ask the parent factory if the bean cannot be found in this factory instance.* @param name the name of the bean to query* @param allowFactoryBeanInit whether a {@code FactoryBean} may get initialized* just for the purpose of determining its object type* @return the type of the bean, or {@code null} if not determinable* @throws NoSuchBeanDefinitionException if there is no bean with the given name* @since 5.2* @see #getBean* @see #isTypeMatch*/确定具有给定名称的bean的类型。更具体地说,确定getBean将为给定名称返回的对象类型。
对于FactoryBean,返回FactoryBean创建的对象类型,如FactoryBean.getObjectType()所示。根据allowFactoryBaininit标志,如果没有可用的早期类型信息,这可能会导致初始化先前未初始化的FactoryBean。@NullableClass<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;/*** Return the aliases for the given bean name, if any.* <p>All of those aliases point to the same bean when used in a {@link #getBean} call.* <p>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.* <p>Will ask the parent factory if the bean cannot be found in this factory instance.* @param name the bean name to check for aliases* @return the aliases, or an empty array if none* @see #getBean*/返回给定bean名称的别名(如果有的话)。
当在getBean调用中使用时,所有这些别名都指向同一个bean。
如果给定的名称是一个别名,那么将返回相应的原始bean名称和其他别名(如果有的话),原始bean名称是数组中的第一个元素。String[] getAliases(String name);}
总结下来为这几个属性和方法。
如果名为myJndiObject的bean是FactoryBean,那么获取&myJndiObject将返回工厂,而不是工厂返回的实例
String FACTORY_BEAN_PREFIX = "&";
通过一些参数寻找获取bean
Object getBean(String name) throws BeansException;
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
<T> T getBean(Class<T> requiredType) throws BeansException;
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
通过一些参数寻找获取bean的Provider
<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
<T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);
判断name这个bean是不是这个工厂生产的
boolean containsBean(String name);
判断这个bean是单例模式还是原型模式。单例就是这个对象全局只有一个。原型就是这个Bean每次被使用都会生成一个新的对象。
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
判断这个bean是否某类型的bean
boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException;
获取这个bean的类型
Class<?> getType(String name) throws NoSuchBeanDefinitionException;
Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;
获取这个bean的别名
String[] getAliases(String name);
先阅读源码,再看下面这个总结,可以知道beanFactory这个接口定义的就是对bean的一些基本操作,查询bean,获取bean,判断bean的存在,bean的类型等操作。