SpringBeans源碼解讀之IOC容器之始祖

spring Ioc容器的實現,從根源上是beanfactory,但真正可以作爲一個可以獨立使用的ioc容器還是DefaultListableBeanFactory,因此可以這麼說,

DefaultListableBeanFactory 是整個spring ioc的始祖,研究透它的前生今世對我們理解spring ioc的概念有着重要的作用。

1. DefaultListableBeanFactory的作用:

默認實現了ListableBeanFactory和BeanDefinitionRegistry接口,基於bean definition對象,是一個成熟的bean factroy。

最典型的應用是:在訪問bean前,先註冊所有的definition(可能從bean definition配置文件中)。使用預先建立的bean定義元數據對象,從本地的bean definition表中查詢bean definition因而將不會花費太多成本。

DefaultListableBeanFactory既可以作爲一個單獨的beanFactory,也可以作爲自定義beanFactory的父類。

注意:特定格式bean definition的解析器可以自己實現,也可以使用原有的解析器,如:

PropertiesBeanDefinitionReader和XmLBeanDefinitionReader。

2. DefaultListableBeanFactory的繼承關係

3. 好了,開始進入DefaultListableBeanFactory的大千世界

直接繼承關係:

3.1 靜態方法

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>有如下作用:

檢索多個實例.

延遲或者選擇性地檢索一個實例.

打破循環依賴.

抽象的scope,可以從一個包含scope的更小的scope中檢索一個實例。

實例代碼:3.2 繼承自AbstractAutowireCapableBeanFactory的方法:

AbstractAutowireCapableBeanFactory的作用:

提供bean的創建 (有construct方法), 屬性注值, 綁定 (包括自動綁定)和初始化.

處理運行時bean引用, 解析管理的集合, 調用初始化方法。

3.2 繼承自AbstractAutowireCapableBeanFactory的方法:

AbstractAutowireCapableBeanFactory的作用:

提供bean的創建 (有construct方法), 屬性注值, 綁定 (包括自動綁定)和初始化.

處理運行時bean引用, 解析管理的集合, 調用初始化方法。

最主要的子類要實現的模板方法是 AutowireCapableBeanFactory.resolveDependency(DependencyDescriptor, String, Set, TypeConverter), 這個方法用來實現類型的自動綁定.

在DefaultListableBeanFactory中實現了AbstractAutowireCapableBeanFactory.copyConfigurationFrom

其中:

allowBeanDefinitionOverriding定義了是否允許同名的不同bean definition再次進行註冊;

allowEagerClassLoading 定義了是否允許eager類(相對於lazy)的加載,甚至延遲初始化的bean的加載。

autowireCandidateResolver是一個策略接口,用來決定一個特定的bean definition 是否滿足做一個特定依賴的自動綁定的候選項,方法如下所示:

resolvableDependencies:定義了依賴類型和其對應的自動綁定值的鍵值對集合。

AbstractAutowireCapableBeanFactory的copyConfigurationFrom方法:

其中,instantiationStrategy 爲創建bean 實例的策略,默認值:

private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();

allowCircularReferences 確定是否自動嘗試去解析循環引用的bean。

ignoredDependencyTypes 定義了在依賴檢查和自動綁定時要忽略的依賴類型,是一組類對象,例如string,默認爲沒有。

ignoredDependencyInterfaces 定義了在依賴檢查和自動綁定時要忽略的依賴接口,是一組類對象,默認情況下,只有beanFactory接口被忽略。

父類AbstractBeanFactory的copyConfigurationFrom的實現如下:

其中

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定義的分析和修改的便利方法,同時也提供了對單例的預實例化。

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對象的方法。

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實例,而不是客戶端通過名稱一個一個的查詢得出所有的實例。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章