1 BeanFactory介绍

BeanFactory是Spring中的根容器接口,所有的容器都从从它继承而来,ApplicationContext中对于BeanDefinition的注册,bean实例的获取都是基于BeanFactory来实现。

BeanFactory使用工厂方法设计模式。

2 BeanFactory源码

通过读源码的doc

  • 这个接口是spring bean容器的根接口,它有一些为了提供特定功能的子接口ListableBeanFactoryConfigurableBeanFactory

  • 实现这个接口的对象持有一系列的 bean definitions,每个bean definition 都有一个唯一的字符串名字。返回的Bean可以是单例的,也可以是独立的(每次都要创建),具体返回什么类型取决于applicationcontext的配置。

  • BeanFactory通过依赖注入来完成配置,通常的手段是用setter或者constructor

  • 通常情况BeanFactory加载的BeanDefinition保存在一个配置资源中,比如XML文件。但是具体存储在哪儿是没有限制的,比如LDAP,XML,properties等等。

  • HierarchicalBeanFactory会先从本上下文找,找不到从父BeanFactory找,且本工厂实例中的bean会覆盖父工厂

  • BeanFactory的实现类应该尽可能支持bean的生命周期方法,比如BeanNameAware,BeanClassLoaderAware,等等。

    对于这些生命周期方法的支持,BeanFacoty没有给出抽象的接口,需要实现类自己去实现

BeanFactory的源码:

public interface BeanFactory {

    // 用来区分FactoryBean和其产生的对象
String FACTORY_BEAN_PREFIX = "&"; // 通过BeanName获取Bean
Object getBean(String name) throws BeansException; // 通过beanName和bean 的Class类型来获取Bean
<T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException; // 增加获取bean的参数
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
boolean containsBean(String name); // bean是否是单例
boolean isSingleton(String name) throws NoSuchBeanDefinitionException; // bean是否是prototype
boolean isPrototype(String name) throws NoSuchBeanDefinitionException; //查询指定了名字的Bean的Class类型是否与指定类型匹配
boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException; // 同上
boolean isTypeMatch(String name, @Nullable Class<?> typeToMatch) throws NoSuchBeanDefinitionException; //获取指定名字bean的Class类型
@Nullable
Class<?> getType(String name) throws NoSuchBeanDefinitionException; // 获取bean的别名
String[] getAliases(String name); }

再看看BeanFactory这个大家族都有哪些成员

BeanFactory 有三个子类接口:ListableBeanFactoryHierarchicalBeanFactoryAutowireCapableBeanFactory,还有一个实现类SimpleJndiBeanFactory

接下里就看看它的这三个儿子:

3 BeanFactory家族

3.1 AutowireCapableBeanFactory

具有自动装配的能力,该类可以填充那些不受spring容器控制的bean。

源码查看:

public interface AutowireCapableBeanFactory extends BeanFactory {

	/**
* 常量,表示内部没有定义自动装配。主要BeanFactoryAware等和注解驱动依赖注入,依然启作用。
*/
int AUTOWIRE_NO = 0; /**
* Constant that indicates autowiring bean properties by name
* (applying to all bean property setters).
* 表示根据name自动装配bean属性(应用与bean所有setter属性)
* @see #createBean
* @see #autowire
* @see #autowireBeanProperties
*/
int AUTOWIRE_BY_NAME = 1; /**
* Constant that indicates autowiring bean properties by type
* (applying to all bean property setters).
* 表示依赖于类型自动装配bean属性(应用与bean所有setter属性)
* @see #createBean
* @see #autowire
* @see #autowireBeanProperties
*/
int AUTOWIRE_BY_TYPE = 2; /**
* Constant that indicates autowiring the greediest constructor that
* can be satisfied (involves resolving the appropriate constructor).
* 表示依赖于构造自动装配(解决构造相关的属性)
* @see #createBean
* @see #autowire
*/
int AUTOWIRE_CONSTRUCTOR = 3; /**
* Constant that indicates determining an appropriate autowire strategy
* through introspection of the bean class.
* 从spring3.o已经废弃,通过bean类的内省机制,确定一个合适的自动装配策略。
* @see #createBean
* @see #autowire
* @deprecated as of Spring 3.0: If you are using mixed autowiring strategies,
* prefer annotation-based autowiring for clearer demarcation of autowiring needs.
*/
@Deprecated
int AUTOWIRE_AUTODETECT = 4; //-------------------------------------------------------------------------
// Typical methods for creating and populating external bean instances
//------------------------------------------------------------------------- /**
* Fully create a new bean instance of the given class.
* 完全创建一个新的给定class的bean实例。
* <p>Performs full initialization of the bean, including all applicable
* {@link BeanPostProcessor BeanPostProcessors}.
* 执行所有bean的初始化操作,包括所有应用层的bean处理器BeanPostProcessors。
* <p>Note: This is intended for creating a fresh instance, populating annotated
* fields and methods as well as applying all standard bean initialization callbacks.
* It does <i>not</> imply traditional by-name or by-type autowiring of properties;
* use {@link #createBean(Class, int, boolean)} for those purposes.
* 注意,这个方法,创建一个新的bean实例,并向所有bean初始化回调一样,处理注解fields和方法。但是,不意味着,
* 依赖name或类型,自动装配属性,为了这个目的可以使用{@link #createBean(Class, int, boolean)}方法。
* @param beanClass the class of the bean to create
* 创建bean的类型
* @return the new bean instance 返回bean的实例
* @throws BeansException if instantiation or wiring failed
* 如果自动装配或初始化失败,则抛出BeansException异常。
*/
<T> T createBean(Class<T> beanClass) throws BeansException; /**
* 在初始化回调处理完后,装配给定的bean实例,比如注解自动装配。
* 注意,方法用于处理注解驱动的fields的方法,比如新的实例,或反序列化实例。但是,不意味着,
* 依赖name或类型,自动装配属性,为了这个目的可以使用 {@link #autowireBeanProperties}方法
* @param existingBean the existing bean instance
* 已经完成标准初始化回调的bean实例
* @throws BeansException if wiring failed
* 如果自动装配失败,则抛出BeansException异常。
*/
void autowireBean(Object existingBean) throws BeansException; /**
* 配置给定原始bean:自动装配bean属性,bean属性值,应用工厂回调,比如{@code setBeanName},{@code setBeanFactory},
* 同时,应用所有bean后处理器,(包括,bean实例包装的原始bean)
* 此方法相当与{@link #initializeBean}与依赖bean定义全配置bean。
* 注意:此方法需要一个的bean定义的name。
* @param existingBean the existing bean instance
* 已经存在的bean实例
* @param beanName the name of the bean, to be passed to it if necessary
* (a bean definition of that name has to be available)
* 如果需要,则传入一个bean定义的name
* @return the bean instance to use, either the original or a wrapped one
*返回bean实例,或是原始实例,或包装后的实例。
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if there is no bean definition with the given name
* 如果没有给定的name对应的bean定义,则抛出NoSuchBeanDefinitionException异常。
* @throws BeansException if the initialization failed
* 如果初始化,则抛出BeansException异常。
* @see #initializeBean
*/
Object configureBean(Object existingBean, String beanName) throws BeansException; /**
* 此方法与上述createBean(Class<T> beanClass)方法,不同的是,控制自动装配的策略,是依赖name还是类型,还是构造。
* Fully create a new bean instance of the given class with the specified
* autowire strategy. All constants defined in this interface are supported here.
* <p>Performs full initialization of the bean, including all applicable
* {@link BeanPostProcessor BeanPostProcessors}. This is effectively a superset
* of what {@link #autowire} provides, adding {@link #initializeBean} behavior.
* @param beanClass the class of the bean to create
* @param autowireMode by name or type, using the constants in this interface
* 依赖于类型还是name,还是构造进行自动装配
* @param dependencyCheck whether to perform a dependency check for objects
* (not applicable to autowiring a constructor, thus ignored there)
* 是否执行依赖检查
* @return the new bean instance
* @throws BeansException if instantiation or wiring failed
* @see #AUTOWIRE_NO
* @see #AUTOWIRE_BY_NAME
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_CONSTRUCTOR
*/
Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException; /**
* 此方法与上述autowireBean(Object existingBean)方法,不同的是,控制自动装配的策略,是依赖name还是类型,还是构造。
* Instantiate a new bean instance of the given class with the specified autowire
* strategy. All constants defined in this interface are supported here.
* Can also be invoked with {@code AUTOWIRE_NO} in order to just apply
* before-instantiation callbacks (e.g. for annotation-driven injection).
* 在初始化回调以前,比如注解驱动注入,为了调整应用,可以传入{@code AUTOWIRE_NO}。
* <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
* callbacks or perform any further initialization of the bean.
* 需要注意的是,此方法不应用bean后处理器回调和进一步的bean初始化。
* This interface offers distinct, fine-grained operations for those purposes, for example
* {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor}
* callbacks are applied, if applicable to the construction of the instance.
* 此方法与#initializeBean方法不同。然而如果使用是构造实例模式,将会调用{@link InstantiationAwareBeanPostProcessor}回调。
* @param beanClass the class of the bean to instantiate
* @param autowireMode by name or type, using the constants in this interface
* @param dependencyCheck whether to perform a dependency check for object
* references in the bean instance (not applicable to autowiring a constructor,
* thus ignored there)
* @return the new bean instance
* @throws BeansException if instantiation or wiring failed
* @see #AUTOWIRE_NO
* @see #AUTOWIRE_BY_NAME
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_CONSTRUCTOR
* @see #AUTOWIRE_AUTODETECT
* @see #initializeBean
* @see #applyBeanPostProcessorsBeforeInitialization
* @see #applyBeanPostProcessorsAfterInitialization
*/
Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException; /**
* 此方主要是自动装配bean的属性。
* Autowire the bean properties of the given bean instance by name or type.
* Can also be invoked with {@code AUTOWIRE_NO} in order to just apply
* after-instantiation callbacks (e.g. for annotation-driven injection).
* 依赖于name和类型自动装配给定bean的属性。 在初始化回调以前,比如注解驱动注入,为了调整应用,可以传入{@code AUTOWIRE_NO}。
* <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
* callbacks or perform any further initialization of the bean. This interface
* offers distinct, fine-grained operations for those purposes, for example
* {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor}
* callbacks are applied, if applicable to the configuration of the instance.
* @param existingBean the existing bean instance
* @param autowireMode by name or type, using the constants in this interface
* @param dependencyCheck whether to perform a dependency check for object
* references in the bean instance
* @throws BeansException if wiring failed
* @see #AUTOWIRE_BY_NAME
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_NO
*/
void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)
throws BeansException; /**
* Apply the property values of the bean definition with the given name to
* the given bean instance. The bean definition can either define a fully
* self-contained bean, reusing its property values, or just property values
* meant to be used for existing bean instances.
* 应用给定name的bean的定义的属性给指定bean实例。bean定义可以是一个完全自包含的bean,重用他的属性,或
* 调整属性,意味着用于已经存在的bean实例。
* <p>This method does <i>not</i> autowire bean properties; it just applies
* explicitly defined property values. Use the {@link #autowireBeanProperties}
* method to autowire an existing bean instance.
* 此方法不会自动装配bean属性,仅仅使用显示定义的bean的属性值。使用 {@link #autowireBeanProperties}方法自动注入
* 已经存在的bean实例。
* <b>Note: This method requires a bean definition for the given name!</b>
* 注意:此方法需要bean定义的给定name。
* <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
* callbacks or perform any further initialization of the bean. This interface
* offers distinct, fine-grained operations for those purposes, for example
* {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor}
* callbacks are applied, if applicable to the configuration of the instance.
* 需要注意的是,此方法不应用bean后处理器回调和进一步的bean初始化。此方法与#initializeBean方法不同。
* 然而如果应用到配置实例,将会调用{@link InstantiationAwareBeanPostProcessor}回调。
* @param existingBean the existing bean instance
* @param beanName the name of the bean definition in the bean factory
* (a bean definition of that name has to be available)
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if there is no bean definition with the given name
* @throws BeansException if applying the property values failed
* @see #autowireBeanProperties
*/
void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException; /**
* Initialize the given raw bean, applying factory callbacks
* such as {@code setBeanName} and {@code setBeanFactory},
* also applying all bean post processors (including ones which
* might wrap the given raw bean).
* 初始化给定的原始bean,应用工厂调用,比如{@code setBeanName} 和 {@code setBeanFactory},
* 同时应有所有bean后处理器,包括包装的指定原始bean。
* <p>Note that no bean definition of the given name has to exist
* in the bean factory. The passed-in bean name will simply be used
* for callbacks but not checked against the registered bean definitions.
* 注意:如果在bean工厂中,必须有给定的name的bean的定义。bean的name仅仅用于回调,并不检查注册bean的定义。
* @param existingBean the existing bean instance
* @param beanName the name of the bean, to be passed to it if necessary
* (only passed to {@link BeanPostProcessor BeanPostProcessors})
* @return the bean instance to use, either the original or a wrapped one
* @throws BeansException if the initialization failed
*/
Object initializeBean(Object existingBean, String beanName) throws BeansException; /**
* Apply {@link BeanPostProcessor BeanPostProcessors} to the given existing bean
* instance, invoking their {@code postProcessBeforeInitialization} methods.
* The returned bean instance may be a wrapper around the original.
* 应用{@link BeanPostProcessor BeanPostProcessors}到给定存在的bean实例,并调用{@code postProcessBeforeInitialization}
* 方法,返回的bean实例,也许是一个原始的包装bean。
* @param existingBean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one
* @throws BeansException if any post-processing failed
* @see BeanPostProcessor#postProcessBeforeInitialization
*/
Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException; /**
* Apply {@link BeanPostProcessor BeanPostProcessors} to the given existing bean
* instance, invoking their {@code postProcessAfterInitialization} methods.
* The returned bean instance may be a wrapper around the original.
* 此方法与上面方法不同是调用bean后处理的{@code postProcessAfterInitialization}。
* @param existingBean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one
* @throws BeansException if any post-processing failed
* @see BeanPostProcessor#postProcessAfterInitialization
*/
Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException; /**
* Destroy the given bean instance (typically coming from {@link #createBean}),
* applying the {@link org.springframework.beans.factory.DisposableBean} contract as well as
* registered {@link DestructionAwareBeanPostProcessor DestructionAwareBeanPostProcessors}.
* <p>Any exception that arises during destruction should be caught
* and logged instead of propagated to the caller of this method.
* 销毁给定bean的实例,比如来自于{@link #createBean})方法创建的bean实例,
* 同时调用{@link org.springframework.beans.factory.DisposableBean},以及注册的
* {@link DestructionAwareBeanPostProcessor DestructionAwareBeanPostProcessors}.
* 在析构的构成中国,任何异常的抛出,将会传播到方法的调用者。
* @param existingBean the bean instance to destroy
*/
void destroyBean(Object existingBean); /**
* Resolve the bean instance that uniquely matches the given object type, if any,
* including its bean name.
* 若果存在,返回唯一匹配自定类型的bean实例,包括bean的name。
* <p>This is effectively a variant of {@link #getBean(Class)} which preserves the
* bean name of the matching instance.
* 此方法等同于{@link #getBean(Class)}方法,并保存bean的name。
* @param requiredType type the bean must match; can be an interface or superclass.
* {@code null} is disallowed.
* 需要匹配的类型,可以是接口或类,但不能为null。
* @return the bean name plus bean instance
* @throws NoSuchBeanDefinitionException if no matching bean was found
* @throws NoUniqueBeanDefinitionException if more than one matching bean was found
* 如果存在多个匹配的bean,则抛出NoUniqueBeanDefinitionException异常
* @throws BeansException if the bean could not be created
* @since 4.3.3
* @see #getBean(Class)
*/
<T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType) throws BeansException; /**
* Resolve the specified dependency against the beans defined in this factory.
* 在工厂根据bean的定义,解决特殊的依赖。
* @param descriptor the descriptor for the dependency (field/method/constructor)
* @param requestingBeanName the name of the bean which declares the given dependency
* @return the resolved object, or {@code null} if none found
* @throws NoSuchBeanDefinitionException if no matching bean was found
* @throws NoUniqueBeanDefinitionException if more than one matching bean was found
* @throws BeansException if dependency resolution failed for any other reason
* @since 2.5
* @see #resolveDependency(DependencyDescriptor, String, Set, TypeConverter)
*/
Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName) throws BeansException; /**
* Resolve the specified dependency against the beans defined in this factory.
* 此方法与上面方法的不同是,多了一个类型转换器
* @param descriptor the descriptor for the dependency (field/method/constructor)
* @param requestingBeanName the name of the bean which declares the given dependency
* @param autowiredBeanNames a Set that all names of autowired beans (used for
* resolving the given dependency) are supposed to be added to
* @param typeConverter the TypeConverter to use for populating arrays and collections
* 用于数组与集合类的转换。
* @return the resolved object, or {@code null} if none found
* @throws NoSuchBeanDefinitionException if no matching bean was found
* @throws NoUniqueBeanDefinitionException if more than one matching bean was found
* @throws BeansException if dependency resolution failed for any other reason
* @since 2.5
* @see DependencyDescriptor
*/
Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName,
Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException; }

AutowireCapableBeanFactory接口,主要提供的创建bean实例,自动装配bean属性,应用bean配置属性,初始化bean,应用bean后处理器 BeanPostProcessor ,解决bean依赖和销毁bean操作。对于自动装配,主要提供了根据bean的name,类型和构造自动装配方式。一般不建议在 在代码中直接使用AutowireCapableBeanFactory接口,我们可以通过应用上下文的ApplicationContext#getAutowireCapableBeanFactory()方法或者通过实现BeanFactoryAware,获取暴露的bean工厂,然后转换为AutowireCapableBeanFactory

3.2 HierarchicalBeanFactory

该类具有层次划分功能,提供了父子容器。

public interface HierarchicalBeanFactory extends BeanFactory {

	/**
* 返回父容器
*/
@Nullable
BeanFactory getParentBeanFactory(); /**
* 本容器是否包含某个bean,不管父容器
*/
boolean containsLocalBean(String name); }

3.2 ListableBeanFactory

这个BeanFactory可以列举所有的bean实例,而不是通过bean的名字一个一个地查找,预先加载所有的BeanDefinition的实现类应该实现这个接口。

实现类如果也实现了HierarchicalBeanFactory,应该只列出本实例的bean,而不要管祖先的factory 中的bean.

public interface ListableBeanFactory extends BeanFactory {

	/**
* 检查本容器是否包含给定beanName的BeanDefinition
*/
boolean containsBeanDefinition(String beanName); /**
* 返回包含BeanDefinition的数量
*/
int getBeanDefinitionCount(); /**
* 返回BeanDifinition的名字
*/
String[] getBeanDefinitionNames(); /**
* Return the names of beans matching the given type (including subclasses),
* judging from either bean definitions or the value of {@code getObjectType}
* in the case of FactoryBeans.
* <p><b>NOTE: This method introspects top-level beans only.</b> It does <i>not</i>
* check nested beans which might match the specified type as well.
* <p>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.
* <p>Does not consider any hierarchy this factory may participate in.
* Use BeanFactoryUtils' {@code beanNamesForTypeIncludingAncestors}
* to include beans in ancestor factories too.
* <p>Note: Does <i>not</i> ignore singleton beans that have been registered
* by other means than bean definitions.
* <p>This version of {@code getBeanNamesForType} matches all kinds of beans,
* be it singletons, prototypes, or FactoryBeans. In most implementations, the
* result will be the same as for {@code getBeanNamesForType(type, true, true)}.
* <p>Bean names returned by this method should always return bean names <i>in the
* order of definition</i> in the backend configuration, as far as possible.
* @param type the generically typed class or interface to match
* @return the names of beans (or objects created by FactoryBeans) matching
* the given object type (including subclasses), or an empty array if none
* @since 4.2
* @see #isTypeMatch(String, ResolvableType)
* @see FactoryBean#getObjectType
* @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors(ListableBeanFactory, ResolvableType)
*/
String[] getBeanNamesForType(ResolvableType type); /**
* 根据类型来返回Bean名称,包含该层的所有Bean,包括FactoryBean
* Return the names of beans matching the given type (including subclasses),
* judging from either bean definitions or the value of {@code getObjectType}
* in the case of FactoryBeans.
* <p><b>NOTE: This method introspects top-level beans only.</b> It does <i>not</i>
* check nested beans which might match the specified type as well.
* <p>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.
* <p>Does not consider any hierarchy this factory may participate in.
* Use BeanFactoryUtils' {@code beanNamesForTypeIncludingAncestors}
* to include beans in ancestor factories too.
* <p>Note: Does <i>not</i> ignore singleton beans that have been registered
* by other means than bean definitions.
* <p>This version of {@code getBeanNamesForType} matches all kinds of beans,
* be it singletons, prototypes, or FactoryBeans. In most implementations, the
* result will be the same as for {@code getBeanNamesForType(type, true, true)}.
* <p>Bean names returned by this method should always return bean names <i>in the
* order of definition</i> in the backend configuration, as far as possible.
* @param type the class or interface to match, or {@code null} for all bean names
* @return the names of beans (or objects created by FactoryBeans) matching
* the given object type (including subclasses), or an empty array if none
* @see FactoryBean#getObjectType
* @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors(ListableBeanFactory, Class)
*/
String[] getBeanNamesForType(@Nullable Class<?> type); /**
* 返回指定类型的名字 includeNonSingletons为false表示只取单例Bean,true则不是
* allowEagerInit为true表示立刻加载,false表示延迟加载
*/
String[] getBeanNamesForType(@Nullable Class<?> type, boolean includeNonSingletons, boolean allowEagerInit); /**
* Return the bean instances that match the given object type (including
* subclasses), judging from either bean definitions or the value of
* {@code getObjectType} in the case of FactoryBeans.
* <p><b>NOTE: This method introspects top-level beans only.</b> It does <i>not</i>
* check nested beans which might match the specified type as well.
* <p>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.
* <p>Does not consider any hierarchy this factory may participate in.
* Use BeanFactoryUtils' {@code beansOfTypeIncludingAncestors}
* to include beans in ancestor factories too.
* <p>Note: Does <i>not</i> ignore singleton beans that have been registered
* by other means than bean definitions.
* <p>This version of getBeansOfType matches all kinds of beans, be it
* singletons, prototypes, or FactoryBeans. In most implementations, the
* result will be the same as for {@code getBeansOfType(type, true, true)}.
* <p>The Map returned by this method should always return bean names and
* corresponding bean instances <i>in the order of definition</i> in the
* backend configuration, as far as possible.
* @param type the class or interface to match, or {@code null} for all concrete beans
* @return a Map with the matching beans, containing the bean names as
* keys and the corresponding bean instances as values
* @throws BeansException if a bean could not be created
* @since 1.1.2
* @see FactoryBean#getObjectType
* @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class)
*/
<T> Map<String, T> getBeansOfType(@Nullable Class<T> type) throws BeansException; /**
* Return the bean instances that match the given object type (including
* subclasses), judging from either bean definitions or the value of
* {@code getObjectType} in the case of FactoryBeans.
* <p><b>NOTE: This method introspects top-level beans only.</b> It does <i>not</i>
* check nested beans which might match the specified type as well.
* <p>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).
* <p>Does not consider any hierarchy this factory may participate in.
* Use BeanFactoryUtils' {@code beansOfTypeIncludingAncestors}
* to include beans in ancestor factories too.
* <p>Note: Does <i>not</i> ignore singleton beans that have been registered
* by other means than bean definitions.
* <p>The Map returned by this method should always return bean names and
* corresponding bean instances <i>in the order of definition</i> in the
* backend configuration, as far as possible.
* @param type the class or interface to match, or {@code null} for all concrete beans
* @param includeNonSingletons whether to include prototype or scoped beans too
* or just singletons (also applies to FactoryBeans)
* @param allowEagerInit whether to initialize <i>lazy-init singletons</i> and
* <i>objects created by FactoryBeans</i> (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.
* @return a Map with the matching beans, containing the bean names as
* keys and the corresponding bean instances as values
* @throws BeansException if a bean could not be created
* @see FactoryBean#getObjectType
* @see BeanFactoryUtils#beansOfTypeIncludingAncestors(ListableBeanFactory, Class, boolean, boolean)
*/
<T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
throws BeansException; /**
* Find all names of beans whose {@code Class} has the supplied {@link Annotation}
* type, without creating any bean instances yet.
* @param annotationType the type of annotation to look for
* @return the names of all matching beans
* @since 4.0
*/
String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType); /**
* Find all beans whose {@code Class} has the supplied {@link Annotation} type,
* returning a Map of bean names with corresponding bean instances.
* @param annotationType the type of annotation to look for
* @return a Map with the matching beans, containing the bean names as
* keys and the corresponding bean instances as values
* @throws BeansException if a bean could not be created
* @since 3.0
*/
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException; /**
* Find an {@link Annotation} of {@code annotationType} on the specified
* bean, traversing its interfaces and super classes if no annotation can be
* found on the given class itself.
* @param beanName the name of the bean to look for annotations on
* @param annotationType the annotation class to look for
* @return the annotation of the given type if found, or {@code null} otherwise
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
* @since 3.0
*/
@Nullable
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)
throws NoSuchBeanDefinitionException; }

Spring-BeanFactory体系介绍的更多相关文章

  1. Spring Cloud体系介绍

    上图只是Spring Cloud体系的一部分,Spring Cloud共集成了19个子项目,里面都包含一个或者多个第三方的组件或者框架! Spring Cloud 工具框架 1.Spring Clou ...

  2. Spring源码分析——BeanFactory体系之抽象类、类分析(二)

    上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry——Spring源码分析——BeanFactory体系之 ...

  3. Spring源码分析——BeanFactory体系之抽象类、类分析(一)

    上一篇介绍了BeanFactory体系的所有接口——Spring源码分析——BeanFactory体系之接口详细分析,本篇就接着介绍BeanFactory体系的抽象类和接口. 一.BeanFactor ...

  4. Spring源码分析——BeanFactory体系之接口详细分析

    Spring的BeanFactory的继承体系堪称经典.这是众所周知的!作为Java程序员,不能错过! 前面的博文分析了Spring的Resource资源类Resouce.今天开始分析Spring的I ...

  5. Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法

    Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法 1. BeanFactory BeanFactory,以Factory结尾,表示它是一个工厂类(接口),用于 ...

  6. springboot:spring data jpa介绍

    转载自:https://www.cnblogs.com/ityouknow/p/5891443.html 在上篇文章springboot(二):web综合开发中简单介绍了一下spring data j ...

  7. spring boot(五)Spring data jpa介绍

    在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...

  8. 最后的记忆——Spring BeanFactory

    本文尝试分析一下Spring 的BeanFactory 体系的 接口设计,尝试理解为什么这么做,为什么接口这么设计.为什么这么去实现,为什么需要有这个方法,为什么 这样命名?接口.类.方法的 大致用途 ...

  9. Spring Data JPA介绍与简单案例

    一.Spring Data JPA介绍 可以理解为JPA规范的再次封装抽象,底层还是使用了Hibernate的JPA技术实现,引用JPQL(Java Persistence Query Languag ...

  10. javaWeb课程体系介绍

    javaWeb课程体系介绍-一般必须学的课程: JavaSE开发Java基础编程Java核心编程Java高级编程DataBase开发MySQLJDBCJavaEE开发Web基础SpringSpring ...

随机推荐

  1. Oracle 查询NULL字段/空字符串

    简单记录一下: 工作中需要查询某个字段值为空的数据, 最开始查询的时候按照以前的思路 : 1.where 字段名=NULL,点击F8,未查到结果: 2.where 字段名='',点击F8,未查到结果: ...

  2. 适配器模式(pthon)

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- # adapter_pattern.py # 适配器模式 class Adaptee: def reque ...

  3. Nginx 添加防爬虫

    include agent_deny.conf; conf下添加 #禁止Scrapy|curl等工具的抓取 if ($http_user_agent ~* (Scrapy|Curl|HttpClien ...

  4. Linux上天之路(十八)之自动化部署

    pexpect Pexpect 是 Don Libes 的 Expect 语言的一个 Python 实现,是一个用来启动子程序,并使用正则表达式对程序输出做出特定响应,以此实现与其自动交互的 Pyth ...

  5. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

  6. Docker之Docker Machine已弃用

    作为刚开始学习docker的新手,从网上查询文档无疑是最快的学习捷径,但是这次在docker翻车了,困扰了整整三天,特记录一下. 一般我们使用docker for windows安装,一路往下点直至安 ...

  7. Flowable实战(七)用户和组

      在流程中,最重要的参与者是用户.流程定义了任务何时需要用户参与,什么用户可以参与.   组可以理解为我们常说的角色.   Flowable中内置了一套简单的对用户和组的支持,身份管理(IDM ID ...

  8. Xbatis:SpringBoot 数据管理框架

    目录 安装 下载源码 编译源码 添加依赖 数据表 数据源 Xbatis XbatisManager Database/Table/Column Column Table Database Create ...

  9. Solon Web 开发,九、跨域处理

    Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...

  10. Android学习笔记5

    SharedPreferenced的使用方法:SharedPreferences 是一个轻量级的存储类,主要是保存一些小的数据,一些状态信息 第一步:初始化         * 获取SharedPre ...