spring Ioc容器的實現,從根源上是beanfactory,但真正能夠做爲一個能夠獨立使用的ioc容器仍是DefaultListableBeanFactory,所以能夠這麼說,
DefaultListableBeanFactory 是整個spring ioc的始祖,研究透它的前生今世對咱們理解spring ioc的概念有着重要的做用。
1. DefaultListableBeanFactory的做用:html
默認實現了ListableBeanFactory和BeanDefinitionRegistry接口,基於bean definition對象,是一個成熟的bean factroy。java
最典型的應用是:在訪問bean前,先註冊全部的definition(可能從bean definition配置文件中)。使用預先創建的bean定義元數據對象,從本地的bean definition表中查詢bean definition於是將不會花費太多成本。spring
DefaultListableBeanFactory既能夠做爲一個單獨的beanFactory,也能夠做爲自定義beanFactory的父類。apache
注意:特定格式bean definition的解析器能夠本身實現,也可使用原有的解析器,如:框架
PropertiesBeanDefinitionReader和XmLBeanDefinitionReader。編輯器
2. DefaultListableBeanFactory的繼承關係ide
(圖片來源:http://www.myexception.cn/software-architecture-design/925888.html)工具
3. 好了,開始進入DefaultListableBeanFactory的大千世界post
直接繼承關係:ui
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable { }
3.1 靜態方法
private static Class<?> javaxInjectProviderClass = null; static { try { javaxInjectProviderClass = ClassUtils.forName("javax.inject.Provider", DefaultListableBeanFactory.class.getClassLoader()); } catch (ClassNotFoundException ex) { // JSR-330 API not available - Provider interface simply not supported then. } }
spring提供一些實用的綜合工具類如ClassUtils,ClassUtils提供了對類的實用方法,主要用在框架內部,想要了解更全面的類的工具方法能夠參考apache commons lang。
Commons Lang The standard Java libraries fail to provide enough methods for manipulation of its core classes. Apache Commons Lang provides these extra methods. Lang provides a host of helper utilities for the java.lang API, notably String manipulation methods, basic numerical methods, object reflection, concurrency, creation and serialization and System properties. Additionally it contains basic enhancements to java.util.Date and a series of utilities dedicated to help with building methods, such as hashCode, toString and equals
ClassUtils.forName方法是class.forname的重寫,返回一個類的實例,區別請參考源碼。
上述靜態方法中返回了javax.inject.Provider的一個實例,那麼咱們揭開javax.inject.Provider的面紗看看它到底起了什麼做用。
語法:public interface Provider<T>
提供了一個 T的實例
. 一般做爲一個依賴注入容器器的父接口. 能夠注入任何類型的 T
, 固然也能夠注入 Provider<T>
. 相對於直接注入 T
,注入 Provider<T>
有以下做用:
實例代碼:
class Car { @Inject Car(Provider<Seat> seatProvider) { Seat driver = seatProvider.get(); Seat passenger = seatProvider.get(); ... } }
3.2 繼承自AbstractAutowireCapableBeanFactory的方法:
AbstractAutowireCapableBeanFactory的做用:
提供bean的建立 (有construct方法), 屬性注值, 綁定 (包括自動綁定)和初始化.
處理運行時bean引用, 解析管理的集合, 調用初始化方法。
最主要的子類要實現的模板方法是 AutowireCapableBeanFactory.resolveDependency(DependencyDescriptor, String, Set, TypeConverter)
, 這個方法用來實現類型的自動綁定.
在DefaultListableBeanFactory中實現了AbstractAutowireCapableBeanFactory.copyConfigurationFrom
@Override public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) { super.copyConfigurationFrom(otherFactory); if (otherFactory instanceof DefaultListableBeanFactory) { DefaultListableBeanFactory otherListableFactory = (DefaultListableBeanFactory) otherFactory; this.allowBeanDefinitionOverriding = otherListableFactory.allowBeanDefinitionOverriding; this.allowEagerClassLoading = otherListableFactory.allowEagerClassLoading; this.autowireCandidateResolver = otherListableFactory.autowireCandidateResolver; this.resolvableDependencies.putAll(otherListableFactory.resolvableDependencies); } }
其中:
allowBeanDefinitionOverriding定義了是否容許同名的不一樣bean definition再次進行註冊;
allowEagerClassLoading 定義了是否容許eager類(相對於lazy)的加載,甚至延遲初始化的bean的加載。
autowireCandidateResolver是一個策略接口,用來決定一個特定的bean definition 是否知足作一個特定依賴的自動綁定的候選項,方法以下所示:
//SimpleAutowireCandidateResolver implements AutowireCandidateResolver
@Override
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
return bdHolder.getBeanDefinition().isAutowireCandidate(); }
//GenericTypeAwareAutowireCandidateResolver implements AutowireCandidateResolver
@Override
public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) {
if (!bdHolder.getBeanDefinition().isAutowireCandidate()) { // if explicitly false, do not proceed with any other checks return false; } return (descriptor == null || checkGenericTypeMatch(bdHolder, descriptor)); }
resolvableDependencies:定義了依賴類型和其對應的自動綁定值的鍵值對集合。
AbstractAutowireCapableBeanFactory的copyConfigurationFrom方法:
@Override public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) { super.copyConfigurationFrom(otherFactory); if (otherFactory instanceof AbstractAutowireCapableBeanFactory) { AbstractAutowireCapableBeanFactory otherAutowireFactory = (AbstractAutowireCapableBeanFactory) otherFactory; this.instantiationStrategy = otherAutowireFactory.instantiationStrategy; this.allowCircularReferences = otherAutowireFactory.allowCircularReferences; this.ignoredDependencyTypes.addAll(otherAutowireFactory.ignoredDependencyTypes); this.ignoredDependencyInterfaces.addAll(otherAutowireFactory.ignoredDependencyInterfaces); } }
其中,instantiationStrategy 爲建立bean 實例的策略,默認值:
private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();
allowCircularReferences 肯定是否自動嘗試去解析循環引用的bean。
ignoredDependencyTypes 定義了在依賴檢查和自動綁定時要忽略的依賴類型,是一組類對象,例如string,默認爲沒有。
ignoredDependencyInterfaces 定義了在依賴檢查和自動綁定時要忽略的依賴接口,是一組類對象,默認狀況下,只有beanFactory接口被忽略。
父類AbstractBeanFactory的copyConfigurationFrom的實現以下:
@Override public void copyConfigurationFrom(ConfigurableBeanFactory otherFactory) { Assert.notNull(otherFactory, "BeanFactory must not be null"); setBeanClassLoader(otherFactory.getBeanClassLoader()); setCacheBeanMetadata(otherFactory.isCacheBeanMetadata()); setBeanExpressionResolver(otherFactory.getBeanExpressionResolver()); if (otherFactory instanceof AbstractBeanFactory) { AbstractBeanFactory otherAbstractFactory = (AbstractBeanFactory) otherFactory; this.customEditors.putAll(otherAbstractFactory.customEditors); this.propertyEditorRegistrars.addAll(otherAbstractFactory.propertyEditorRegistrars); this.beanPostProcessors.addAll(otherAbstractFactory.beanPostProcessors); this.hasInstantiationAwareBeanPostProcessors = this.hasInstantiationAwareBeanPostProcessors || otherAbstractFactory.hasInstantiationAwareBeanPostProcessors; this.hasDestructionAwareBeanPostProcessors = this.hasDestructionAwareBeanPostProcessors || otherAbstractFactory.hasDestructionAwareBeanPostProcessors; this.scopes.putAll(otherAbstractFactory.scopes); this.securityContextProvider = otherAbstractFactory.securityContextProvider; } else { setTypeConverter(otherFactory.getTypeConverter()); } }
其中
customEditors 是自定義的屬性編輯器,適用於該beanFactory的全部bean。
propertyEditorRegistrars屬性編輯器的註冊器,使用於該beanFactory的全部bean。
beanPostProcessors 建立bean時應用的beanPostProcessors。
hasInstantiationAwareBeanPostProcessors 代表是否註冊有任何的InstantiationAwareBeanPostProcessors。
hasDestructionAwareBeanPostProcessors代表是否註冊有任何的DestructionAwareBeanPostProcessors
scopes:域標識符和對應域的鍵值對集合。
securityContextProvider 運行SecurityManager時使用的security context。
3.3 繼承自ListableBeanFactory接口的方法
ListableBeanFactory是beanFactory接口的擴展接口,它能夠枚舉全部的bean實例,而不是客戶端經過名稱一個一個的查詢得出全部的實例。要預加載全部的bean定義的beanfactory能夠實現這個接口來。該 接口定義了訪問容器中Bean基本信息的若干方法,如查看Bean的個數、獲取某一類型Bean的配置名、查看容器中是否包括某一Bean等方法;
containsBeanDefinition(String beanName) Check if this bean factory contains a bean definition with the given name. findAnnotationOnBean(String beanName, Class<A> annotationType) Find a Annotation of annotationType on the specified bean, traversing its interfaces and super classes if no annotation can be found on the given class itself. getBeanDefinitionCount() Return the number of beans defined in the factory. getBeanDefinitionNames() Return the names of all beans defined in this factory. getBeanNamesForType(Class<?> type) Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans. getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) Return the names of beans matching the given type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans. getBeansOfType(Class<T> type) Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans. getBeansOfType(Class<T> 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 getObjectType in the case of FactoryBeans. getBeansWithAnnotation(Class<? extends Annotation> annotationType) Find all beans whose Class has the supplied Annotation type.
3.4 繼承自ConfigurableListableBeanFactory接口的方法
ConfigurableListableBeanFactory 它同時繼承了ListableBeanFactory,AutowireCapableBeanFactory和ConfigurableBeanFactory,提供了對bean定義的分析和修改的便利方法,同時也提供了對單例的預實例化。
void freezeConfiguration() Freeze all bean definitions, signalling that the registered bean definitions will not be modified or post-processed any further. BeanDefinition getBeanDefinition(String beanName) Return the registered BeanDefinition for the specified bean, allowing access to its property values and constructor argument value (which can be modified during bean factory post-processing). void ignoreDependencyInterface(Class<?> ifc) Ignore the given dependency interface for autowiring. void ignoreDependencyType(Class<?> type) Ignore the given dependency type for autowiring: for example, String. boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor) Determine whether the specified bean qualifies as an autowire candidate, to be injected into other beans which declare a dependency of matching type. boolean isConfigurationFrozen() Return whether this factory's bean definitions are frozen, i.e. void preInstantiateSingletons() Ensure that all non-lazy-init singletons are instantiated, also considering FactoryBeans. void registerResolvableDependency(Class<?> dependencyType, Object autowiredValue) Register a special dependency type with corresponding autowired value.
3.5 繼承自BeanDefinitionRegistry接口的方法
BeanDefinitionRegistry:Spring配置文件中每個<bean>節點元素在Spring容器裏都經過一個BeanDefinition對象表示,它描述了Bean的配置信息。而BeanDefinition Registry接口提供了向容器手工註冊BeanDefinition對象的方法。
boolean containsBeanDefinition(String beanName) Check if this registry contains a bean definition with the given name. BeanDefinition getBeanDefinition(String beanName) Return the BeanDefinition for the given bean name. int getBeanDefinitionCount() Return the number of beans defined in the registry. String[] getBeanDefinitionNames() Return the names of all beans defined in this registry. boolean isBeanNameInUse(String beanName) Determine whether the given bean name is already in use within this registry, i.e. void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) Register a new bean definition with this registry. void removeBeanDefinition(String beanName) Remove the BeanDefinition for the given name.
3.6 序列化支持
private void writeObject(java.io.ObjectOutputStream out)throws IOException private void readObject(java.io.ObjectInputStream in)throws IOException, ClassNotFoundException; private void readObjectNoData()throws ObjectStreamException;
4 小結:
spring Ioc容器的實現,從根源上是beanfactory,但真正能夠做爲一個能夠獨立使用的ioc容器仍是DefaultListableBeanFactory,所以能夠這麼說,
DefaultListableBeanFactory 是整個spring ioc的始祖,研究透它的前生今世對咱們理解spring ioc的概念有着重要的做用。
DefaultListableBeanFactory功能的實現是經過實現特定功能的接口來完成。
AbstractAutowireCapableBeanFactory 實現屬性的自動綁定功能。
ConfigurableListableBeanFactory提供了對bean定義的分析和修改的便利方法,同時也提供了對單例的預實例化。
ListableBeanFactory提供枚舉全部的bean實例,而不是客戶端經過名稱一個一個的查詢得出全部的實例。
BeanDefinitionRegistry 提供了beanDefinition的管理。