ListableBeanFactory接口
ListableBeanFactory獲取bean時(shí),Spring 鼓勵(lì)使用這個(gè)接口定義的api. 還有個(gè)Beanfactory方便使用.其他的4個(gè)接口都是不鼓勵(lì)使用的.
提供容器中bean迭代的功能,不再需要一個(gè)個(gè)bean地查找.比如可以一次獲取全部的bean(太暴力了),根據(jù)類型獲取bean.在看SpringMVC時(shí),掃描包路徑下的具體實(shí)現(xiàn)策略就是使用的這種方式(那邊使用的是BeanFactoryUtils封裝的api).
如果同時(shí)實(shí)現(xiàn)了HierarchicalBeanFactory,返回值不會(huì)考慮父類BeanFactory,只考慮當(dāng)前factory定義的類.當(dāng)然也可以使用BeanFactoryUtils輔助類來查找祖先工廠中的類. 即ListableBeanFactory是beanFactory接口的擴(kuò)展接口,它可以枚舉所有的bean實(shí)例,而不是客戶端通過名稱一個(gè)一個(gè)的查詢得出所有的實(shí)例。要預(yù)加載所有的bean定義的beanfactory可以實(shí)現(xiàn)這個(gè)接口來。該 接口定義了訪問容器中Bean基本信息的若干方法,如查看Bean的個(gè)數(shù)、獲取某一類型Bean的配置名、查看容器中是否包括某一Bean等方法.
這個(gè)接口中的方法只會(huì)考慮本factory定義的bean.這些方法會(huì)忽略ConfigurableBeanFactory的registerSingleton注冊的單例bean(getBeanNamesOfType和getBeansOfType是例外,一樣會(huì)考慮手動(dòng)注冊的單例).當(dāng)然BeanFactory的getBean一樣可以透明訪問這些特殊bean.當(dāng)然在典型情況下,所有的bean都是由external bean定義,所以應(yīng)用不需要顧慮這些差別.
注意:getBeanDefinitionCount和containsBeanDefinition的實(shí)現(xiàn)方法因?yàn)樾时容^低,還是少用為好.
ListableBeanFactory源碼具體:
1、3個(gè)跟BeanDefinition有關(guān)的總體操作。包括BeanDefinition的總數(shù)、名字的集合、指定類型的名字的集合。(這里指出,BeanDefinition是Spring中非常重要的一個(gè)類,每個(gè)BeanDefinition實(shí)例都包含一個(gè)類在Spring工廠中所有屬性。)
2、2個(gè)getBeanNamesForType重載方法。根據(jù)指定類型(包括子類)獲取其對(duì)應(yīng)的所有Bean名字。
3、2個(gè)getBeansOfType重載方法。根據(jù)類型(包括子類)返回指定Bean名和Bean的Map。
4、2個(gè)跟注解查找有關(guān)的方法。根據(jù)注解類型,查找Bean名和Bean的Map。以及根據(jù)指定Bean名和注解類型查找指定的Bean。
總結(jié):
正如這個(gè)工廠接口的名字所示,這個(gè)工廠接口最大的特點(diǎn)就是可以列出工廠可以生產(chǎn)的所有實(shí)例。當(dāng)然,工廠并沒有直接提供返回所有實(shí)例的方法,也沒這個(gè)必要。它可以返回指定類型的所有的實(shí)例。而且你可以通過getBeanDefinitionNames()得到工廠所有bean的名字,然后根據(jù)這些名字得到所有的Bean。這個(gè)工廠接口擴(kuò)展了BeanFactory的功能,作為上文指出的BeanFactory二級(jí)接口,有9個(gè)獨(dú)有的方法,擴(kuò)展了跟BeanDefinition的功能,提供了BeanDefinition、BeanName、注解有關(guān)的各種操作。它可以根據(jù)條件返回Bean的集合,這就是它名字的由來——ListableBeanFactory。
/*** * @author DemoTransfer* @since 4.3*/ public interface ListableBeanFactory extends BeanFactory {//-------------------------------------------------------------------------// 暴力獲取全部bean的屬性//-------------------------------------------------------------------------/*** Check if this bean factory contains a bean definition with the given name.* <p>Does not consider any hierarchy this factory may participate in,* and ignores any singleton beans that have been registered by* other means than bean definitions.* @param beanName the name of the bean to look for* @return if this bean factory contains a bean definition with the given name* @see #containsBean*/// 對(duì)于給定的名字是否含有BeanDefinitionboolean containsBeanDefinition(String beanName);/*** Return the number of beans defined in the factory.* <p>Does not consider any hierarchy this factory may participate in,* and ignores any singleton beans that have been registered by* other means than bean definitions.* @return the number of beans defined in the factory*/// 返回工廠的BeanDefinition總數(shù)int getBeanDefinitionCount();/*** Return the names of all beans defined in this factory.* <p>Does not consider any hierarchy this factory may participate in,* and ignores any singleton beans that have been registered by* other means than bean definitions.* @return the names of all beans defined in this factory,* or an empty array if none defined*/// 返回工廠中所有Bean的名字String[] getBeanDefinitionNames();//-------------------------------------------------------------------------// 根據(jù)bean 的類型獲取bean//// 這邊的方法僅檢查頂級(jí)bean.它不會(huì)檢查嵌套的bean.FactoryBean創(chuàng)建的bean會(huì)匹配為FactoryBean而不是原始類型.// 一樣不會(huì)考慮父factory中的bean,非要用可以通過BeanFactoryUtils中的beanNamesForTypeIncludingAncestors.// 其他方式注冊的單例這邊會(huì)納入判斷.// 這個(gè)版本的getBeanNamesForType會(huì)匹配所有類型的bean,包括單例,原型,FactoryBean.返回的bean names會(huì)根據(jù)backend 配置的進(jìn)行排序.//-------------------------------------------------------------------------/*** 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* @since 4.2* @see #isTypeMatch(String, ResolvableType)* @see FactoryBean#getObjectType* @see BeanFactoryUtils#beanNamesForTypeIncludingAncestors(ListableBeanFactory, ResolvableType)*/String[] getBeanNamesForType(ResolvableType type);/*** 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)*/// 獲取給定類型的bean names(包括子類),通過bean 定義或者FactoryBean的getObjectType判斷.// 返回對(duì)于指定類型Bean(包括子類)的所有名字String[] getBeanNamesForType(Class<?> type);/*** 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 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 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>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* @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 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, boolean, boolean)*//** 返回指定類型的名字 includeNonSingletons為false表示只取單例Bean,true則不是* allowEagerInit為true表示立刻加載,false表示延遲加載。 注意:FactoryBeans都是立刻加載的。*/String[] getBeanNamesForType(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)*/// 如果保護(hù)懶加載的類,FactoryBean初始化的類和工廠方法初始化的類會(huì)被初始化.就是說執(zhí)行這個(gè)方法會(huì)執(zhí)行對(duì)應(yīng)的初始化.// 根據(jù)類型(包括子類)返回指定Bean名和Bean的Map<T> Map<String, T> getBeansOfType(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(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*/// 根據(jù)注解類型,查找所有有這個(gè)注解的Bean名和Bean的MapMap<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;//-------------------------------------------------------------------------// 查找一個(gè)類上的注解,如果找不到,父類,接口使用注解也算.//-------------------------------------------------------------------------/*** 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}* @throws NoSuchBeanDefinitionException if there is no bean with the given name* @since 3.0*/// 根據(jù)指定Bean名和注解類型查找指定的Bean<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType)throws NoSuchBeanDefinitionException;}總結(jié)
以上是生活随笔為你收集整理的ListableBeanFactory接口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 日历仿IOS,基于And
- 下一篇: configurablebeanfact