歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring源碼解析 - ListableBeanFactory

Spring源碼解析 - ListableBeanFactory

日期:2017/3/1 9:18:10   编辑:Linux編程

Extension of the {@link BeanFactory} interface to be implemented by bean factories
that can enumerate all their bean instances, rather than attempting bean lookup
by name one by one as requested by clients. BeanFactory implementations that
preload all their bean definitions (such as XML-based factories) may implement
this interface.

擴展BeanFactory接口,提供所有bean 實例的枚舉,不再需要客戶端通過一個個bean name查找.BeanFactory實現類預加載bean定義(如通過實現xml的工廠)需要實現這個接口。

If this is a {@link HierarchicalBeanFactory}, the return values will <i>not</i>
take any BeanFactory hierarchy into account, but will relate only to the beans
defined in the current factory. Use the {@link BeanFactoryUtils} helper class
to consider beans in ancestor factories too.
如果一樣實現了HierarchicalBeanFactory,返回值不會考慮父類BeanFactory,只考慮當前factory定義的類.當然也可以使用BeanFactoryUtils輔助類來查找祖先工廠中的類.

The methods in this interface will just respect bean definitions of this factory.
They will ignore any singleton beans that have been registered by other means like
{@link org.springframework.beans.factory.config.ConfigurableBeanFactory}'s
{@code registerSingleton} method, with the exception of
{@code getBeanNamesOfType} and {@code getBeansOfType} which will check
such manually registered singletons too. Of course, BeanFactory's {@code getBean}
does allow transparent access to such special beans as well. However, in typical
scenarios, all beans will be defined by external bean definitions anyway, so most
applications don't need to worry about this differentation.

這個接口中的方法只會考慮本factory定義的bean.這些方法會忽略ConfigurableBeanFactory的registerSingleton注冊的單例bean,getBeanNamesOfType和getBeansOfType是例外,一樣會考慮手動注冊的單例.當然BeanFactory的getBean一樣可以透明訪問這些特殊bean.當然在典型情況下,所有的bean都是由external bean定義,所以應用不需要顧慮這些差別.
<b>NOTE:</b> With the exception of {@code getBeanDefinitionCount}
and {@code containsBeanDefinition}, the methods in this interface
are not designed for frequent invocation. Implementations may be slow.

注意:getBeanDefinitionCount和containsBeanDefinition的實現方法因為效率比較低,並不是供頻繁調用的.

package org.springframework.beans.factory;
public interface ListableBeanFactory extends BeanFactory {

/**
* 檢查bean factory是否含有給定name的bean定義.
* 忽略父factory和其他factory注冊的單例bean
* @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
*/
boolean containsBeanDefinition(String beanName);

/**
* factory中定義的bean數量
* 一樣不考慮父factory和其他factory注冊的單例bean
* @return the number of beans defined in the factory
*/
int getBeanDefinitionCount();

/**
* 獲取工廠中定義的所有bean 的name
* 一樣不考慮父factory和其他factory注冊的單例bean
* @return the names of all beans defined in this factory,
* or an empty array if none defined
*/
String[] getBeanDefinitionNames();

/**
* 獲取給定類型的bean names(包括子類),通過bean 定義或者FactoryBean的getObjectType判斷.
* 注意:這個方法僅檢查頂級bean.它不會檢查嵌套的bean.
* FactoryBean創建的bean會匹配為FactoryBean而不是原始類型.
* 一樣不會考慮父factory中的bean,可以使用BeanFactoryUtils中的beanNamesForTypeIncludingAncestors.
* 其他方式注冊的單例這邊會納入判斷.
* 這個版本的getBeanNamesForType會匹配所有類型的bean,包括單例,原型,FactoryBean.在大多數實現中返回結果跟getBeanNamesOfType(type,true,true)一樣.
* 返回的bean names會根據backend 配置的進行排序.
* @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(Class<?> type);

/**
*
* @param type the class or interface to match, or {@code null} for all bean names
* @param includeNonSingletons是否只要單例(包括BeanFactory),還是原型或其他作用域的bean一樣包括
* @param allowEagerInit 是否初始化懶加載的單例,FactoryBean初始化的類和工廠方法初始化的類.就是說執行這個方法會執行對應的初始化.
* @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)
*/
String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit);

/**
*
* @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(Class<T> type) throws BeansException;

/**
*
* @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;

/**
* 找到使用注解的類.
* @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
*/
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
throws BeansException;

/**
* 查找一個類上的注解,如果找不到,父類,接口使用注解也算.
* @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 found, or {@code null}
*/
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType);

}

Spring中如何配置Hibernate事務 http://www.linuxidc.com/Linux/2013-12/93681.htm

Struts2整合Spring方法及原理 http://www.linuxidc.com/Linux/2013-12/93692.htm

基於 Spring 設計並實現 RESTful Web Services http://www.linuxidc.com/Linux/2013-10/91974.htm

Spring-3.2.4 + Quartz-2.2.0集成實例 http://www.linuxidc.com/Linux/2013-10/91524.htm

使用 Spring 進行單元測試 http://www.linuxidc.com/Linux/2013-09/89913.htm

運用Spring注解實現Netty服務器端UDP應用程序 http://www.linuxidc.com/Linux/2013-09/89780.htm

Spring 3.x 企業應用開發實戰 PDF完整高清掃描版+源代碼 http://www.linuxidc.com/Linux/2013-10/91357.htm

Spring 的詳細介紹:請點這裡
Spring 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved