BeanFactory
是Spring中的根容器接口,全部的容器都從從它繼承而來,ApplicationContext
中對於BeanDefinition
的註冊,bean
實例的獲取都是基於BeanFactory
來實現。java
BeanFactory
使用工廠方法設計模式。spring
經過讀源碼的doc
,設計模式
spring bean
容器的根接口,它有一些爲了提供特定功能的子接口ListableBeanFactory
和ConfigurableBeanFactory
bean definitions
,每一個bean definition
都有一個惟一的字符串名字。返回的Bean
能夠是單例的,也能夠是獨立的(每次都要建立),具體返回什麼類型取決於applicationcontext
的配置。BeanFactory
經過依賴注入來完成配置,一般的手段是用setter
或者constructor
BeanFactory
加載的BeanDefinition
保存在一個配置資源中,好比XML文件。可是具體存儲在哪兒是沒有限制的,好比LDAP
,XML
,properties
等等。HierarchicalBeanFactory
會先從本上下文找,找不到從父BeanFactory
找,且本工廠實例中的bean會覆蓋父工廠BeanFactory
的實現類應該儘量支持bean
的生命週期方法,好比BeanNameAware
,BeanClassLoaderAware
,等等。對於這些生命週期方法的支持,BeanFacoty
沒有給出抽象的接口,須要實現類本身去實現數組
BeanFactory
的源碼:app
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
這個你們族都有哪些成員ide
BeanFactory
有三個子類接口:ListableBeanFactory
、HierarchicalBeanFactory
和AutowireCapableBeanFactory
,還有一個實現類SimpleJndiBeanFactory
。post
接下里就看看它的這三個兒子:ui
具備自動裝配的能力,該類能夠填充那些不受spring容器控制的bean。this
源碼查看:spa
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
。
該類具備層次劃分功能,提供了父子容器。
public interface HierarchicalBeanFactory extends BeanFactory { /** * 返回父容器 */ @Nullable BeanFactory getParentBeanFactory(); /** * 本容器是否包含某個bean,無論父容器 */ boolean containsLocalBean(String name); }
這個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; }