zoukankan      html  css  js  c++  java
  • Spring-BeanFactory体系介绍

    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;
    
    }
    
    
  • 相关阅读:
    64位整数乘法
    HTML中常见问题汇总贴
    题解 牛客【「水」悠悠碧波】
    题解 CF1391B 【Fix You】
    四级-句子
    快速幂||取余运算
    最大子列和
    JvavScript中的函数与对象
    JavaScript中的流程控制语句
    冒泡排序,选择排序,插入排序,归并排序
  • 原文地址:https://www.cnblogs.com/watertreestar/p/12782415.html
Copyright © 2011-2022 走看看