javascript
Spring IoC容器设计原理及高级特性
文章目錄
- Spring IoC容器概述
- IoC容器系列的設計與實現:BeanFactory和ApplicationContext
- BeanFactory
- BeanFactory容器的設計原理
- ApplicationContext
- ApplicationContext容器的設計原理
- IoC容器的初始化過程
- IoC容器的依賴注入
- 依賴注入發生的時間
- 依賴注入的流程圖
- 依賴注入的源碼分析
- 容器其他相關特性的設計與實現
- Application中Bean的初始化及銷毀
- lazy-init屬性和預實例化
- FactoryBean的實現
- BeanPostProcessor的實現
- autowiring(自動裝配的實現)
- Bean的依賴檢查
Spring IoC容器概述
IoC是Spring容器的內核,其字面意思是控制反轉。那么究竟是哪些方面的控制被反轉了?其實是依賴對象的獲得被反轉了。基于此,提出了DI(依賴注入)的概念。
IoC容器系列的設計與實現:BeanFactory和ApplicationContext
在Spring IoC容器的設計中,主要有兩個容器系列,一個是實現BeanFactory接口餓簡單容器系列,這系列容器只實現了容器的最基本功能;另一個是ApplicationContext應用上下文,它作為容器的高級形態而存在。應用上下文在簡單容器的基礎上,增加了許多面向框架的特性,同時對應用環境做了許多適配。
Spring通過定義BeanDefinition來管理基于Spring的應用中的各種對象以及它們之間的相互依賴關系。對IoC容器來說,BeanDefinition就是依賴反轉模式中管理的對象依賴關系的數據抽象,也是容器實現依賴反轉功能的核心數據結構,依賴反轉功能都是圍繞這個BeanDefinition的處理來完成的。
? 從接口BeanFactory到HierarchicalBeanFactory,再到ConfigurableBeanFactory,是一條主要的BeanFactory設計路徑
BeanFactory定義了基本的IoC容器的規范。包括了getBean()(通過這個方法可以從容器中取得Bean)。
HierarchicalBeanFactory接口在繼承了BeanFactory后,增加了getParentBeanFactory(),使BeanFactory具備了雙親IoC容器的管理功能。
在接下來的ConfigurableBeanFactory中,定義了一些對BeanFactory的配置功能,比如通過setParentBeanFactory()設置雙親IoC容器,通過addBeanPostProcessor()配置Bean后置處理器,等等。
? 第二條接口設計主線是,以ApplicationContext為核心的接口設計
我們常用的應用上下文基本上都是ConfigurableApplicationContext或者WebApplicationContext的實現
在這個接口體系中,ListableBeanFactory和HierarchicalBeanFactory兩個接口,連接BeanFactory接口定義和ApplicationConext應用上下文的接口定義。
在ListableBeanFactory接口中,細化了許多BeanFactory的接口功能,比如定義了getBeanDefinitionNames()接口方法;對于HierarchicalBeanFactory接口,我們在前文中已經提到過;對于ApplicationContext接口,它通過繼承MessageSource、ResourceLoader、ApplicationEventPublisher接口,在BeanFactory簡單IoC容器的基礎上添加了許多對高級容器的特性的支持
? 這里涉及的是主要接口關系,而具體的IoC容器都是在這個接口體系下實現的,比如DefaultListableBeanFactory,這個基本IoC容器的實現就是實現了ConfigurableBeanFactory,從而成為一個簡單IoC容器的實現。
像其他IoC容器,比如XmlBeanFactory,都是在DefaultListableBeanFactory的基礎上做擴展
? 這個接口系統是以BeanFactory和ApplicationContext為核心
而BeanFactory又是IoC容器的最基本接口,在ApplicationContext的設計中,一方面,可以看到它繼承了BeanFactory接口體系的接口,具備了BeanFactory IoC容器的基本功能
另一方面,通過繼承MessageSource、ResourceLoadr、ApplicationEventPublisher這些接口,BeanFactory為ApplicationContext賦予了更高級的IoC容器特性。
對于ApplicationContext而言,為了在Web環境中使用它,還設計了WebApplicationContext接口,而這個接口通過繼承ThemeSource接口來擴充功能。
BeanFactory
Spring Bean的創建是典型的工廠模式,這一系列的Bean工廠,也即IOC容器為開發者管理對象間的依賴關系提供了很多便利和基礎服務,在Spring中有許多的IOC容器的實現供用戶選擇和使用,其相互關系如下:
BeanFactory接口定義了IoC容器最基本的形式,不關心 Bean 是怎樣定義和加載的。如果我們想要知道一個工廠具體產生對象的過程,則要看這個接口的實現類。
其中BeanFactory作為最頂層的一個接口類,它定義了IOC容器的基本功能規范,BeanFactory 有三個子類:
ListableBeanFactory、HierarchicalBeanFactory 和AutowireCapableBeanFactory。
但是從上圖中我們可以發現最終的默認實現類是 DefaultListableBeanFactory,他實現了所有的接口。那為何要定義這么多層次的接口呢?查閱這些接口的源碼和說明發現,每個接口都有他使用的場合,它主要是為了區分在 Spring 內部在操作過程中對象的傳遞和轉化過程中,對對象的數據訪問所做的限制。例如 ListableBeanFactory 接口表示這些 Bean 是可列表的,而 HierarchicalBeanFactory 表示子容器可以通過接口方法訪問父容器,AutowireCapableBeanFactory 接口定義 Bean 的自動裝配規則。這四個接口共同定義了 Bean 的集合、Bean 之間的關系、以及 Bean 行為。
最基本的IOC容器接口BeanFactory
public interface BeanFactory { //對FactoryBean的轉義定義,因為如果使用bean的名字檢索FactoryBean得到的對象是工廠生成的對象, //如果需要得到工廠本身,需要轉義 String FACTORY_BEAN_PREFIX = "&"; //根據bean的名字,獲取在IOC容器中得到bean實例 Object getBean(String name) throws BeansException; //根據bean的名字和Class類型來得到bean實例,增加了類型安全驗證機制。 Object getBean(String name, Class requiredType) throws BeansException; //提供對bean的檢索,看看是否在IOC容器有這個名字的bean boolean containsBean(String name); //根據bean名字得到bean實例,并同時判斷這個bean是不是單例 boolean isSingleton(String name) throws NoSuchBeanDefinitionException; //得到bean實例的Class類型 Class getType(String name) throws NoSuchBeanDefinitionException; //得到bean的別名,如果根據別名檢索,那么其原名也會被檢索出來 String[] getAliases(String name); }BeanFactory容器的設計原理
在BeanFactory接口的基礎上,Spring提供了一系列容器的實現供開發人員使用。我們以XmlBeanFactory的實現為例來說明簡單IoC容器的設計原理。
XmlBeanFactory繼承自DefaultListableBeanFactory這個類,同時實現了其他諸如XML讀取的附加功能。即它是一個可以讀取以XML文件方式定義的BeanDefinition的IoC容器。
在XmlBeanFactory中,初始化了一個XmlBeanDefinitionReader對象,由它來完成XML形式的信息處理。構造XmlBeanFactory這個容器時,需要指定BeanDefinition的信息來源,將它封裝成Spring中的Resource類來給出。然后傳遞給XmlBeanFactory構造函數,IoC容器就可以方便地定位到需要的BeanDefinition信息來對Bean完成容器的初始化和依賴注入過程。對XmlBeanDefinitionReader對象的初始化,以及使用這個對象來完成對loadBeanDefinition的調用,就是這個調用啟動從Resource中載入BeanDefinitions的過程。
ApplicationContext
ApplicationContext是Spring提供的一個高級的IoC容器,它除了能夠提供IoC容器的基本功能外,還為用戶提供了以下的附加服務。
ApplicationContext容器的設計原理
我們以常用的FileSystemXmlApplicationContext的實現為例說明ApplicationContext容器的設計原理。
public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException { super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); } }在FileSystemXmlApplicationContext的設計中,我們看到ApplicationContext應用上下文的主要功能已經在FileSystemXmlApplicationContext的基類AbstractXmlApplicationContext中實現了,FileSystemXmlApplicationContext作為一個具體的應用上下文,只需要實現和它自身設計相關的兩個功能。一個功能是,如果應用直接使用FileSystemXmlApplicationContext,對于實例化這個應用上下文的支持,同時啟動IoC容器的refresh()過程。另一個功能是與FileSystemXmlApplicationContext設計具體相關的功能,這部分與怎樣從文件系統中加載XML的Bean定義資源有關。
IoC容器的初始化過程
IoC容器的初始化是由refresh()方法來啟動的,這個方法標志著容器的正式啟動。具體來說,這個啟動包括BeanDefinition的Resource定位、載入和注冊三個基本過程。
- Resource定位:指的是BeanDefinition的資源定位,它由ResourceLoader通過統一的Resource接口來完成。
- BeanDefinition的載入:把用戶定義好的Bea表示成IoC容器內部的數據結構,而這個容器內部的數據結構就是BeanDefinition。
- BeanDefinition的注冊:通過調用BeanDefinitionRegistry接口的實現來完成的。這個注冊過程是把載入過程中解析得到的BeanDefinition向IoC容器進行注冊。
IoC容器的依賴注入
依賴注入發生的時間
當Spring IoC容器完成了Bean定義資源的定位、載入和解析注冊以后,IoC容器中已經管理類Bean定義的相關數據,即初始化過程完成的主要工作是在IoC容器中建立BeanDefinition數據映射。
但是此時IoC容器還沒有對所管理的Bean進行依賴注入,依賴注入在以下兩種情況發生:
(1) 用戶第一次通過getBean方法向IoC容索要Bean時,IoC容器觸發依賴注入。
(2) 當用戶在Bean定義資源中為< Bean >元素配置了lazy-init屬性,即讓容器在解析注冊Bean定義時進行預實例化,觸發依賴注入。
BeanFactory接口定義了Spring IoC容器的基本功能規范,是Spring IoC容器所應遵守的最底層和最基本的編程規范。BeanFactory接口中定義了幾個getBean方法,就是用戶向IoC容器索取管理的Bean的方法,我們通過分析其子類的具體實現,理解Spring IoC容器在用戶索取Bean時如何完成依賴注入。
依賴注入的流程圖
依賴注入的源碼分析
在BeanFactory中我們看到getBean()函數,它的具體實現在AbstractBeanFactory中。
AbstractBeanFactory的getBean相關方法的源碼如下:
//獲取IoC容器中指定名稱的Bean public Object getBean(String name) throws BeansException { //doGetBean才是真正向IoC容器獲取被管理Bean的過程 return doGetBean(name, null, null, false); } //獲取IoC容器中指定名稱和類型的Bean public <T> T getBean(String name, Class<T> requiredType) throws BeansException { //doGetBean才是真正向IoC容器獲取被管理Bean的過程 return doGetBean(name, requiredType, null, false); } //獲取IoC容器中指定名稱和參數的Bean public Object getBean(String name, Object... args) throws BeansException { //doGetBean才是真正向IoC容器獲取被管理Bean的過程 return doGetBean(name, null, args, false); } //獲取IoC容器中指定名稱、類型和參數的Bean public <T> T getBean(String name, Class<T> requiredType, Object... args) throws BeansException { //doGetBean才是真正向IoC容器獲取被管理Bean的過程 return doGetBean(name, requiredType, args, false); } //真正實現向IoC容器獲取Bean的功能,也是觸發依賴注入功能的地方 @SuppressWarnings("unchecked") protected <T> T doGetBean( final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { //根據指定的名稱獲取被管理Bean的名稱,剝離指定名稱中對容器的相關依賴 //如果指定的是別名,將別名轉換為規范的Bean名稱 final String beanName = transformedBeanName(name); Object bean; //先從緩存中取是否已經有被創建過的單例類型的Bean,對于單例模式的Bean整 //個IoC容器中只創建一次,不需要重復創建 Object sharedInstance = getSingleton(beanName); //IoC容器創建單例模式Bean實例對象 if (sharedInstance != null && args == null) { if (logger.isDebugEnabled()) { //如果指定名稱的Bean在容器中已有單例模式的Bean被創建,直接返回 //已經創建的Bean if (isSingletonCurrentlyInCreation(beanName)) { logger.debug("Returning eagerly cached instance of singleton bean '" + beanName + "' that is not fully initialized yet - a consequence of a circular reference"); } else { logger.debug("Returning cached instance of singleton bean '" + beanName + "'"); } } //獲取給定Bean的實例對象,主要是完成FactoryBean的相關處理 //注意:BeanFactory是管理容器中Bean的工廠,而FactoryBean是 //創建創建對象的工廠Bean,兩者之間有區別 bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else {//緩存沒有正在創建的單例模式Bean //緩存中已經有已經創建的原型模式Bean,但是由于循環引用的問題導致實 //例化對象失敗 if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } //對IoC容器中是否存在指定名稱的BeanDefinition進行檢查,首先檢查是否 //能在當前的BeanFactory中獲取的所需要的Bean,如果不能則委托當前容器 //的父級容器去查找,如果還是找不到則沿著容器的繼承體系向父級容器查找 BeanFactory parentBeanFactory = getParentBeanFactory(); //當前容器的父級容器存在,且當前容器中不存在指定名稱的Bean if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { //解析指定Bean名稱的原始名稱 String nameToLookup = originalBeanName(name); if (args != null) { //委派父級容器根據指定名稱和顯式的參數查找 return (T) parentBeanFactory.getBean(nameToLookup, args); } else { //委派父級容器根據指定名稱和類型查找 return parentBeanFactory.getBean(nameToLookup, requiredType); } } //創建的Bean是否需要進行類型驗證,一般不需要 if (!typeCheckOnly) { //向容器標記指定的Bean已經被創建 markBeanAsCreated(beanName); } //根據指定Bean名稱獲取其父級的Bean定義,主要解決Bean繼承時子類 //合并父類公共屬性問題 final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); //獲取當前Bean所有依賴Bean的名稱 String[] dependsOn = mbd.getDependsOn(); //如果當前Bean有依賴Bean if (dependsOn != null) { for (String dependsOnBean : dependsOn) { //遞歸調用getBean方法,獲取當前Bean的依賴Bean getBean(dependsOnBean); //把被依賴Bean注冊給當前依賴的Bean registerDependentBean(dependsOnBean, beanName); } } //創建單例模式Bean的實例對象 if (mbd.isSingleton()) { //這里使用了一個匿名內部類,創建Bean實例對象,并且注冊給所依賴的對象 sharedInstance = getSingleton(beanName, new ObjectFactory() { public Object getObject() throws BeansException { try { //創建一個指定Bean實例對象,如果有父級繼承,則合并子//類和父類的定義 return createBean(beanName, mbd, args); } catch (BeansException ex) { //顯式地從容器單例模式Bean緩存中清除實例對象 destroySingleton(beanName); throw ex; } } }); //獲取給定Bean的實例對象 bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } //IoC容器創建原型模式Bean實例對象 else if (mbd.isPrototype()) { //原型模式(Prototype)是每次都會創建一個新的對象 Object prototypeInstance = null; try { //回調beforePrototypeCreation方法,默認的功能是注冊當前創//建的原型對象 beforePrototypeCreation(beanName); //創建指定Bean對象實例 prototypeInstance = createBean(beanName, mbd, args); } finally { //回調afterPrototypeCreation方法,默認的功能告訴IoC容器指//定Bean的原型對象不再創建了 afterPrototypeCreation(beanName); } //獲取給定Bean的實例對象 bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } //要創建的Bean既不是單例模式,也不是原型模式,則根據Bean定義資源中 //配置的生命周期范圍,選擇實例化Bean的合適方法,這種在Web應用程序中 //比較常用,如:request、session、application等生命周期 else { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); //Bean定義資源中沒有配置生命周期范圍,則Bean定義不合法 if (scope == null) { throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'"); } try { //這里又使用了一個匿名內部類,獲取一個指定生命周期范圍的實例 Object scopedInstance = scope.get(beanName, new ObjectFactory() { public Object getObject() throws BeansException { beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } } }); //獲取給定Bean的實例對象 bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Scope '" + scopeName + "' is not active for the current thread; " + "consider defining a scoped proxy for this bean if you intend to refer to it from a singleton", ex); } } } //對創建的Bean實例對象進行類型檢查 if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return (T) bean; }通過上面對向IoC容器獲取Bean方法的分析,我們可以看到在Spring中,如果Bean定義的單例模式(Singleton),則容器在創建之前先從緩存中查找,以確保整個容器中只存在一個實例對象。如果Bean定義的是原型模式(Prototype),則容器每次都會創建一個新的實例對象。除此之外,Bean定義還可以擴展為指定其生命周期范圍。
上面的源碼只是定義了根據Bean定義的模式,采取的不同創建Bean實例對象的策略,具體的Bean實例對象的創建過程由實現了ObejctFactory接口的匿名內部類的createBean方法完成,ObejctFactory使用委派模式,具體的Bean實例創建過程交由其實現類AbstractAutowireCapableBeanFactory完成,我們繼續分析AbstractAutowireCapableBeanFactory的createBean方法的源碼,理解其創建Bean實例的具體實現過程。
AbstractAutowireCapableBeanFactory類實現了ObejctFactory接口,創建容器指定的Bean實例對象,同時還對創建的Bean實例對象進行初始化處理。其創建Bean實例對象的方法源碼如下:
//創建Bean實例對象 protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) throws BeanCreationException { if (logger.isDebugEnabled()) { logger.debug("Creating instance of bean '" + beanName + "'"); } //判斷需要創建的Bean是否可以實例化,即是否可以通過當前的類加載器加載 resolveBeanClass(mbd, beanName); //校驗和準備Bean中的方法覆蓋 try { mbd.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { //如果Bean配置了初始化前和初始化后的處理器,則試圖返回一個需要創建//Bean的代理對象 Object bean = resolveBeforeInstantiation(beanName, mbd); if (bean != null) { return bean; } } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "BeanPostProcessor before instantiation of bean failed", ex); } //創建Bean的入口 Object beanInstance = doCreateBean(beanName, mbd, args); if (logger.isDebugEnabled()) { logger.debug("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; } //真正創建Bean的方法 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) { //封裝被創建的Bean對象 BeanWrapper instanceWrapper = null; if (mbd.isSingleton()){//單例模式的Bean,先從容器中緩存中獲取同名Bean instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { //創建實例對象 instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null); //獲取實例化對象的類型 Class beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null); //調用PostProcessor后置處理器 synchronized (mbd.postProcessingLock) { if (!mbd.postProcessed) { applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); mbd.postProcessed = true; } } // Eagerly cache singletons to be able to resolve circular references //向容器中緩存單例模式的Bean對象,以防循環引用 boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences && isSingletonCurrentlyInCreation(beanName)); if (earlySingletonExposure) { if (logger.isDebugEnabled()) { logger.debug("Eagerly caching bean '" + beanName + "' to allow for resolving potential circular references"); } //這里是一個匿名內部類,為了防止循環引用,盡早持有對象的引用 addSingletonFactory(beanName, new ObjectFactory() { public Object getObject() throws BeansException { return getEarlyBeanReference(beanName, mbd, bean); } }); } //Bean對象的初始化,依賴注入在此觸發 //這個exposedObject在初始化完成之后返回作為依賴注入完成后的Bean Object exposedObject = bean; try { //將Bean實例對象封裝,并且Bean定義中配置的屬性值賦值給實例對象 populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { //初始化Bean對象 exposedObject = initializeBean(beanName, exposedObject, mbd); } } catch (Throwable ex) { if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) { throw (BeanCreationException) ex; } else { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex); } } if (earlySingletonExposure) { //獲取指定名稱的已注冊的單例模式Bean對象 Object earlySingletonReference = getSingleton(beanName, false); if (earlySingletonReference != null) { //根據名稱獲取的以注冊的Bean和正在實例化的Bean是同一個 if (exposedObject == bean) { //當前實例化的Bean初始化完成 exposedObject = earlySingletonReference; } //當前Bean依賴其他Bean,并且當發生循環引用時不允許新創建實例對象 else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) { String[] dependentBeans = getDependentBeans(beanName); Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length); //獲取當前Bean所依賴的其他Bean for (String dependentBean : dependentBeans) { //對依賴Bean進行類型檢查 if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) { actualDependentBeans.add(dependentBean); } } if (!actualDependentBeans.isEmpty()) { throw new BeanCurrentlyInCreationException(beanName, "Bean with name '" + beanName + "' has been injected into other beans [" + StringUtils.collectionToCommaDelimitedString(actualDependentBeans) + "] in its raw version as part of a circular reference, but has eventually been " + "wrapped. This means that said other beans do not use the final version of the " + "bean. This is often the result of over-eager type matching - consider using " + "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example."); } } } } //注冊完成依賴注入的Bean try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }通過對方法源碼的分析,我們看到具體的依賴注入實現在以下兩個方法中:
(1) createBeanInstance:生成Bean所包含的java對象實例。
(2) populateBean :對Bean屬性的依賴注入進行處理。
下面繼續分析這兩個方法的代碼實現。
在createBeanInstance方法中,根據指定的初始化策略,使用靜態工廠、工廠方法或者容器的自動裝配特性生成java實例對象,創建對象的源碼如下:
//創建Bean的實例對象 protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) { //檢查確認Bean是可實例化的 Class beanClass = resolveBeanClass(mbd, beanName); //使用工廠方法對Bean進行實例化 if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Bean class isn't public, and non-public access not allowed: " + beanClass.getName()); } if (mbd.getFactoryMethodName() != null) { //調用工廠方法實例化 return instantiateUsingFactoryMethod(beanName, mbd, args); } //使用容器的自動裝配方法進行實例化 boolean resolved = false; boolean autowireNecessary = false; if (args == null) { synchronized (mbd.constructorArgumentLock) { if (mbd.resolvedConstructorOrFactoryMethod != null) { resolved = true; autowireNecessary = mbd.constructorArgumentsResolved; } } } if (resolved) { if (autowireNecessary) { //配置了自動裝配屬性,使用容器的自動裝配實例化 //容器的自動裝配是根據參數類型匹配Bean的構造方法 return autowireConstructor(beanName, mbd, null, null); } else { //使用默認的無參構造方法實例化 return instantiateBean(beanName, mbd); } } //使用Bean的構造方法進行實例化 Constructor[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { //使用容器的自動裝配特性,調用匹配的構造方法實例化 return autowireConstructor(beanName, mbd, ctors, args); } //使用默認的無參構造方法實例化 return instantiateBean(beanName, mbd); } //使用默認的無參構造方法實例化Bean對象 protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) { try { Object beanInstance; final BeanFactory parent = this; //獲取系統的安全管理接口,JDK標準的安全管理API if (System.getSecurityManager() != null) { //這里是一個匿名內置類,根據實例化策略創建實例對象 beanInstance = AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { return getInstantiationStrategy().instantiate(mbd, beanName, parent); } }, getAccessControlContext()); } else { //將實例化的對象封裝起來 beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, parent); } BeanWrapper bw = new BeanWrapperImpl(beanInstance); initBeanWrapper(bw); return bw; } catch (Throwable ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex); } }經過對上面的代碼分析,我們可以看出,對使用工廠方法和自動裝配特性的Bean的實例化相對比較清楚,調用相應的工廠方法或者參數匹配的構造方法即可完成實例化對象的工作,但是對于我們最常使用的默認無參構造方法就需要使用相應的初始化策略(JDK的反射機制或者CGLIB)來進行初始化了,在方法getInstantiationStrategy().instantiate中就具體實現類使用初始策略實例化對象。
在使用默認的無參構造方法創建Bean的實例化對象時,方法getInstantiationStrategy().instantiate調用了SimpleInstantiationStrategy類中的實例化Bean的方法,其源碼如下:
//使用初始化策略實例化Bean對象 public Object instantiate(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) { //如果Bean定義中沒有方法覆蓋,則就不需要CGLIB父類類的方法 if (beanDefinition.getMethodOverrides().isEmpty()) { Constructor<?> constructorToUse; synchronized (beanDefinition.constructorArgumentLock) { //獲取對象的構造方法或工廠方法 constructorToUse = (Constructor<?>) beanDefinition.resolvedConstructorOrFactoryMethod; //如果沒有構造方法且沒有工廠方法 if (constructorToUse == null) { //使用JDK的反射機制,判斷要實例化的Bean是否是接口 final Class clazz = beanDefinition.getBeanClass(); if (clazz.isInterface()) { throw new BeanInstantiationException(clazz, "Specified class is an interface"); } try { if (System.getSecurityManager() != null) { //這里是一個匿名內置類,使用反射機制獲取Bean的構造方法 constructorToUse = AccessController.doPrivileged(new PrivilegedExceptionAction<Constructor>() { public Constructor run() throws Exception { return clazz.getDeclaredConstructor((Class[]) null); } }); } else { constructorToUse = clazz.getDeclaredConstructor((Class[]) null); } beanDefinition.resolvedConstructorOrFactoryMethod = constructorToUse; } catch (Exception ex) { throw new BeanInstantiationException(clazz, "No default constructor found", ex); } } } //使用BeanUtils實例化,通過反射機制調用”構造方法.newInstance(arg)”來進行實例化 return BeanUtils.instantiateClass(constructorToUse); } else { //使用CGLIB來實例化對象 return instantiateWithMethodInjection(beanDefinition, beanName, owner); } }通過上面的代碼分析,我們看到了如果Bean有方法被覆蓋了,則使用JDK的反射機制進行實例化,否則,使用CGLIB進行實例化。
instantiateWithMethodInjection方法調用SimpleInstantiationStrategy的子類CglibSubclassingInstantiationStrategy使用CGLIB來進行初始化,其源碼如下:
//使用CGLIB進行Bean對象實例化 public Object instantiate(Constructor ctor, Object[] args) { //CGLIB中的類 Enhancer enhancer = new Enhancer(); //將Bean本身作為其基類 enhancer.setSuperclass(this.beanDefinition.getBeanClass()); enhancer.setCallbackFilter(new CallbackFilterImpl()); enhancer.setCallbacks(new Callback[] { NoOp.INSTANCE, new LookupOverrideMethodInterceptor(), new ReplaceOverrideMethodInterceptor() }); //使用CGLIB的create方法生成實例對象 return (ctor == null) ? enhancer.create() : enhancer.create(ctor.getParameterTypes(), args); }CGLIB是一個常用的字節碼生成器的類庫,它提供了一系列API實現java字節碼的生成和轉換功能。我們在學習JDK的動態代理時都知道,JDK的動態代理只能針對接口,如果一個類沒有實現任何接口,要對其進行動態代理只能使用CGLIB。
在第2步的分析中我們已經了解到Bean的依賴注入分為以下兩個過程:
(1) createBeanInstance:生成Bean所包含的java對象實例。
(2) populateBean :對Bean屬性的依賴注入進行處理。
第3、4步中我們已經分析了容器初始化生成Bean所包含的Java實例對象的過程,現在我們繼續分析生成對象后,Spring IoC容器是如何將Bean的屬性依賴關系注入Bean實例對象中并設置好的,屬性依賴注入的代碼如下:
//將Bean屬性設置到生成的實例對象上 protected void populateBean(String beanName, AbstractBeanDefinition mbd, BeanWrapper bw) { //獲取容器在解析Bean定義資源時為BeanDefiniton中設置的屬性值 PropertyValues pvs = mbd.getPropertyValues(); //實例對象為null if (bw == null) { //屬性值不為空 if (!pvs.isEmpty()) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance"); } else { //實例對象為null,屬性值也為空,不需要設置屬性值,直接返回 return; } } //在設置屬性之前調用Bean的PostProcessor后置處理器 boolean continueWithPropertyPopulation = true; if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { continueWithPropertyPopulation = false; break; } } } } if (!continueWithPropertyPopulation) { return; } //依賴注入開始,首先處理autowire自動裝配的注入 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); //對autowire自動裝配的處理,根據Bean名稱自動裝配注入 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) { autowireByName(beanName, mbd, bw, newPvs); } //根據Bean類型自動裝配注入 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { autowireByType(beanName, mbd, bw, newPvs); } pvs = newPvs; } //檢查容器是否持有用于處理單態模式Bean關閉時的后置處理器 boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors(); //Bean實例對象沒有依賴,即沒有繼承基類 boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE); if (hasInstAwareBpps || needsDepCheck) { //從實例對象中提取屬性描述符 PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw); if (hasInstAwareBpps) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; //使用BeanPostProcessor處理器處理屬性值 pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvs == null) { return; } } } } if (needsDepCheck) { //為要設置的屬性進行依賴檢查 checkDependencies(beanName, mbd, filteredPds, pvs); } } //對屬性進行注入 applyPropertyValues(beanName, mbd, bw, pvs); } //解析并注入依賴屬性的過程 protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) { if (pvs == null || pvs.isEmpty()) { return; } //封裝屬性值 MutablePropertyValues mpvs = null; List<PropertyValue> original; if (System.getSecurityManager()!= null) { if (bw instanceof BeanWrapperImpl) { //設置安全上下文,JDK安全機制 ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext()); } } if (pvs instanceof MutablePropertyValues) { mpvs = (MutablePropertyValues) pvs; //屬性值已經轉換 if (mpvs.isConverted()) { try { //為實例化對象設置屬性值 bw.setPropertyValues(mpvs); return; } catch (BeansException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Error setting property values", ex); } } //獲取屬性值對象的原始類型值 original = mpvs.getPropertyValueList(); } else { original = Arrays.asList(pvs.getPropertyValues()); } //獲取用戶自定義的類型轉換 TypeConverter converter = getCustomTypeConverter(); if (converter == null) { converter = bw; } //創建一個Bean定義屬性值解析器,將Bean定義中的屬性值解析為Bean實例對象 //的實際值 BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter); //為屬性的解析值創建一個拷貝,將拷貝的數據注入到實例對象中 List<PropertyValue> deepCopy = new ArrayList<PropertyValue>(original.size()); boolean resolveNecessary = false; for (PropertyValue pv : original) { //屬性值不需要轉換 if (pv.isConverted()) { deepCopy.add(pv); } //屬性值需要轉換 else { String propertyName = pv.getName(); //原始的屬性值,即轉換之前的屬性值 Object originalValue = pv.getValue(); //轉換屬性值,例如將引用轉換為IoC容器中實例化對象引用 Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue); //轉換之后的屬性值 Object convertedValue = resolvedValue; //屬性值是否可以轉換 boolean convertible = bw.isWritableProperty(propertyName) && !PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName); if (convertible) { //使用用戶自定義的類型轉換器轉換屬性值 convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter); } //存儲轉換后的屬性值,避免每次屬性注入時的轉換工作 if (resolvedValue == originalValue) { if (convertible) { //設置屬性轉換之后的值 pv.setConvertedValue(convertedValue); } deepCopy.add(pv); } //屬性是可轉換的,且屬性原始值是字符串類型,且屬性的原始類型值不是 //動態生成的字符串,且屬性的原始值不是集合或者數組類型 else if (convertible && originalValue instanceof TypedStringValue && !((TypedStringValue) originalValue).isDynamic() && !(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) { pv.setConvertedValue(convertedValue); deepCopy.add(pv); } else { resolveNecessary = true; //重新封裝屬性的值 deepCopy.add(new PropertyValue(pv, convertedValue)); } } } if (mpvs != null && !resolveNecessary) { //標記屬性值已經轉換過 mpvs.setConverted(); } //進行屬性依賴注入 try { bw.setPropertyValues(new MutablePropertyValues(deepCopy)); } catch (BeansException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Error setting property values", ex); } }分析上述代碼,我們可以看出,對屬性的注入過程分以下兩種情況:
(1).屬性值類型不需要轉換時,不需要解析屬性值,直接準備進行依賴注入。
(2).屬性值需要進行類型轉換時,如對其他對象的引用等,首先需要解析屬性值,然后對解析后的屬性值進行依賴注入。
對屬性值的解析是在BeanDefinitionValueResolver類中的resolveValueIfNecessary方法中進行的,對屬性值的依賴注入是通過bw.setPropertyValues方法實現的,在分析屬性值的依賴注入之前,我們先分析一下對屬性值的解析過程。
當容器在對屬性進行依賴注入時,如果發現屬性值需要進行類型轉換,如屬性值是容器中另一個Bean實例對象的引用,則容器首先需要根據屬性值解析出所引用的對象,然后才能將該引用對象注入到目標實例對象的屬性上去,對屬性進行解析的由resolveValueIfNecessary方法實現,其源碼如下:
//解析屬性值,對注入類型進行轉換 public Object resolveValueIfNecessary(Object argName, Object value) { //對引用類型的屬性進行解析 if (value instanceof RuntimeBeanReference) { RuntimeBeanReference ref = (RuntimeBeanReference) value; //調用引用類型屬性的解析方法 return resolveReference(argName, ref); } //對屬性值是引用容器中另一個Bean名稱的解析 else if (value instanceof RuntimeBeanNameReference) { String refName = ((RuntimeBeanNameReference) value).getBeanName(); refName = String.valueOf(evaluate(refName)); //從容器中獲取指定名稱的Bean if (!this.beanFactory.containsBean(refName)) { throw new BeanDefinitionStoreException( "Invalid bean name '" + refName + "' in bean reference for " + argName); } return refName; } //對Bean類型屬性的解析,主要是Bean中的內部類 else if (value instanceof BeanDefinitionHolder) { BeanDefinitionHolder bdHolder = (BeanDefinitionHolder) value; return resolveInnerBean(argName, bdHolder.getBeanName(), bdHolder.getBeanDefinition()); } else if (value instanceof BeanDefinition) { BeanDefinition bd = (BeanDefinition) value; return resolveInnerBean(argName, "(inner bean)", bd); } //對集合數組類型的屬性解析 else if (value instanceof ManagedArray) { ManagedArray array = (ManagedArray) value; //獲取數組的類型 Class elementType = array.resolvedElementType; if (elementType == null) { //獲取數組元素的類型 String elementTypeName = array.getElementTypeName(); if (StringUtils.hasText(elementTypeName)) { try { //使用反射機制創建指定類型的對象 elementType = ClassUtils.forName(elementTypeName, this.beanFactory.getBeanClassLoader()); array.resolvedElementType = elementType; } catch (Throwable ex) { throw new BeanCreationException( this.beanDefinition.getResourceDescription(), this.beanName, "Error resolving array type for " + argName, ex); } } //沒有獲取到數組的類型,也沒有獲取到數組元素的類型,則直接設置數 //組的類型為Object else { elementType = Object.class; } } //創建指定類型的數組 return resolveManagedArray(argName, (List<?>) value, elementType); } //解析list類型的屬性值 else if (value instanceof ManagedList) { return resolveManagedList(argName, (List<?>) value); } //解析set類型的屬性值 else if (value instanceof ManagedSet) { return resolveManagedSet(argName, (Set<?>) value); } //解析map類型的屬性值 else if (value instanceof ManagedMap) { return resolveManagedMap(argName, (Map<?, ?>) value); } //解析props類型的屬性值,props其實就是key和value均為字符串的map else if (value instanceof ManagedProperties) { Properties original = (Properties) value; //創建一個拷貝,用于作為解析后的返回值 Properties copy = new Properties(); for (Map.Entry propEntry : original.entrySet()) { Object propKey = propEntry.getKey(); Object propValue = propEntry.getValue(); if (propKey instanceof TypedStringValue) { propKey = evaluate((TypedStringValue) propKey); } if (propValue instanceof TypedStringValue) { propValue = evaluate((TypedStringValue) propValue); } copy.put(propKey, propValue); } return copy; } //解析字符串類型的屬性值 else if (value instanceof TypedStringValue) { TypedStringValue typedStringValue = (TypedStringValue) value; Object valueObject = evaluate(typedStringValue); try { //獲取屬性的目標類型 Class<?> resolvedTargetType = resolveTargetType(typedStringValue); if (resolvedTargetType != null) { //對目標類型的屬性進行解析,遞歸調用 return this.typeConverter.convertIfNecessary(valueObject, resolvedTargetType); } //沒有獲取到屬性的目標對象,則按Object類型返回 else { return valueObject; } } catch (Throwable ex) { throw new BeanCreationException( this.beanDefinition.getResourceDescription(), this.beanName, "Error converting typed String value for " + argName, ex); } } else { return evaluate(value); } } //解析引用類型的屬性值 private Object resolveReference(Object argName, RuntimeBeanReference ref) { try { //獲取引用的Bean名稱 String refName = ref.getBeanName(); refName = String.valueOf(evaluate(refName)); //如果引用的對象在父類容器中,則從父類容器中獲取指定的引用對象 if (ref.isToParent()) { if (this.beanFactory.getParentBeanFactory() == null) { throw new BeanCreationException( this.beanDefinition.getResourceDescription(), this.beanName, "Can't resolve reference to bean '" + refName + "' in parent factory: no parent factory available"); } return this.beanFactory.getParentBeanFactory().getBean(refName); } //從當前的容器中獲取指定的引用Bean對象,如果指定的Bean沒有被實例化 //則會遞歸觸發引用Bean的初始化和依賴注入 else { Object bean = this.beanFactory.getBean(refName); //將當前實例化對象的依賴引用對象 this.beanFactory.registerDependentBean(refName, this.beanName); return bean; } } catch (BeansException ex) { throw new BeanCreationException( this.beanDefinition.getResourceDescription(), this.beanName, "Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex); } } //解析array類型的屬性 private Object resolveManagedArray(Object argName, List<?> ml, Class elementType) { //創建一個指定類型的數組,用于存放和返回解析后的數組 Object resolved = Array.newInstance(elementType, ml.size()); for (int i = 0; i < ml.size(); i++) { //遞歸解析array的每一個元素,并將解析后的值設置到resolved數組中,索引為i Array.set(resolved, i, resolveValueIfNecessary(new KeyedArgName(argName, i), ml.get(i))); } return resolved; } //解析list類型的屬性 private List resolveManagedList(Object argName, List<?> ml) { List<Object> resolved = new ArrayList<Object>(ml.size()); for (int i = 0; i < ml.size(); i++) { //遞歸解析list的每一個元素 resolved.add( resolveValueIfNecessary(new KeyedArgName(argName, i), ml.get(i))); } return resolved; } //解析set類型的屬性 private Set resolveManagedSet(Object argName, Set<?> ms) { Set<Object> resolved = new LinkedHashSet<Object>(ms.size()); int i = 0; //遞歸解析set的每一個元素 for (Object m : ms) { resolved.add(resolveValueIfNecessary(new KeyedArgName(argName, i), m)); i++; } return resolved; } //解析map類型的屬性 private Map resolveManagedMap(Object argName, Map<?, ?> mm) { Map<Object, Object> resolved = new LinkedHashMap<Object, Object>(mm.size()); //遞歸解析map中每一個元素的key和value for (Map.Entry entry : mm.entrySet()) { Object resolvedKey = resolveValueIfNecessary(argName, entry.getKey()); Object resolvedValue = resolveValueIfNecessary( new KeyedArgName(argName, entry.getKey()), entry.getValue()); resolved.put(resolvedKey, resolvedValue); } return resolved; }通過上面的代碼分析,我們明白了Spring是如何將引用類型,內部類以及集合類型等屬性進行解析的,屬性值解析完成后就可以進行依賴注入了,依賴注入的過程就是Bean對象實例設置到它所依賴的Bean對象屬性上去,在第7步中我們已經說過,依賴注入是通過bw.setPropertyValues方法實現的,該方法也使用了委托模式,在BeanWrapper接口中至少定義了方法聲明,依賴注入的具體實現交由其實現類BeanWrapperImpl來完成,下面我們就分析依BeanWrapperImpl中賴注入相關的源碼。
BeanWrapperImpl類主要是對容器中完成初始化的Bean實例對象進行屬性的依賴注入,即把Bean對象設置到它所依賴的另一個Bean的屬性中去,依賴注入的相關源碼如下:
//實現屬性依賴注入功能 private void setPropertyValue(PropertyTokenHolder tokens, PropertyValue pv) throws BeansException { //PropertyTokenHolder主要保存屬性的名稱、路徑,以及集合的size等信息 String propertyName = tokens.canonicalName; String actualName = tokens.actualName; //keys是用來保存集合類型屬性的size if (tokens.keys != null) { //將屬性信息拷貝 PropertyTokenHolder getterTokens = new PropertyTokenHolder(); getterTokens.canonicalName = tokens.canonicalName; getterTokens.actualName = tokens.actualName; getterTokens.keys = new String[tokens.keys.length - 1]; System.arraycopy(tokens.keys, 0, getterTokens.keys, 0, tokens.keys.length - 1); Object propValue; try { //獲取屬性值,該方法內部使用JDK的內省( Introspector)機制,調用屬性//的getter(readerMethod)方法,獲取屬性的值 propValue = getPropertyValue(getterTokens); } catch (NotReadablePropertyException ex) { throw new NotWritablePropertyException(getRootClass(), this.nestedPath + propertyName, "Cannot access indexed value in property referenced " + "in indexed property path '" + propertyName + "'", ex); } //獲取集合類型屬性的長度 String key = tokens.keys[tokens.keys.length - 1]; if (propValue == null) { throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + propertyName, "Cannot access indexed value in property referenced " + "in indexed property path '" + propertyName + "': returned null"); } //注入array類型的屬性值 else if (propValue.getClass().isArray()) { //獲取屬性的描述符 PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName); //獲取數組的類型 Class requiredType = propValue.getClass().getComponentType(); //獲取數組的長度 int arrayIndex = Integer.parseInt(key); Object oldValue = null; try { //獲取數組以前初始化的值 if (isExtractOldValueForEditor()) { oldValue = Array.get(propValue, arrayIndex); } //將屬性的值賦值給數組中的元素 Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType, new PropertyTypeDescriptor(pd, new MethodParameter(pd.getReadMethod(), -1), requiredType)); Array.set(propValue, arrayIndex, convertedValue); } catch (IndexOutOfBoundsException ex) { throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName, "Invalid array index in property path '" + propertyName + "'", ex); } } //注入list類型的屬性值 else if (propValue instanceof List) { PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName); //獲取list集合的類型 Class requiredType = GenericCollectionTypeResolver.getCollectionReturnType( pd.getReadMethod(), tokens.keys.length); List list = (List) propValue; //獲取list集合的size int index = Integer.parseInt(key); Object oldValue = null; if (isExtractOldValueForEditor() && index < list.size()) { oldValue = list.get(index); } //獲取list解析后的屬性值 Object convertedValue = convertIfNecessary(propertyName, oldValue, pv.getValue(), requiredType, new PropertyTypeDescriptor(pd, new MethodParameter(pd.getReadMethod(), -1), requiredType)); if (index < list.size()) { //為list屬性賦值 list.set(index, convertedValue); } //如果list的長度大于屬性值的長度,則多余的元素賦值為null else if (index >= list.size()) { for (int i = list.size(); i < index; i++) { try { list.add(null); } catch (NullPointerException ex) { throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName, "Cannot set element with index " + index + " in List of size " + list.size() + ", accessed using property path '" + propertyName + "': List does not support filling up gaps with null elements"); } } list.add(convertedValue); } } //注入map類型的屬性值 else if (propValue instanceof Map) { PropertyDescriptor pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName); //獲取map集合key的類型 Class mapKeyType = GenericCollectionTypeResolver.getMapKeyReturnType( pd.getReadMethod(), tokens.keys.length); //獲取map集合value的類型 Class mapValueType = GenericCollectionTypeResolver.getMapValueReturnType( pd.getReadMethod(), tokens.keys.length); Map map = (Map) propValue; //解析map類型屬性key值 Object convertedMapKey = convertIfNecessary(null, null, key, mapKeyType, new PropertyTypeDescriptor(pd, new MethodParameter(pd.getReadMethod(), -1), mapKeyType)); Object oldValue = null; if (isExtractOldValueForEditor()) { oldValue = map.get(convertedMapKey); } //解析map類型屬性value值 Object convertedMapValue = convertIfNecessary( propertyName, oldValue, pv.getValue(), mapValueType, new TypeDescriptor(new MethodParameter(pd.getReadMethod(), -1, tokens.keys.length + 1))); //將解析后的key和value值賦值給map集合屬性 map.put(convertedMapKey, convertedMapValue); } else { throw new InvalidPropertyException(getRootClass(), this.nestedPath + propertyName, "Property referenced in indexed property path '" + propertyName + "' is neither an array nor a List nor a Map; returned value was [" + pv.getValue() + "]"); } } //對非集合類型的屬性注入 else { PropertyDescriptor pd = pv.resolvedDescriptor; if (pd == null || !pd.getWriteMethod().getDeclaringClass().isInstance(this.object)) { pd = getCachedIntrospectionResults().getPropertyDescriptor(actualName); //無法獲取到屬性名或者屬性沒有提供setter(寫方法)方法 if (pd == null || pd.getWriteMethod() == null) { //如果屬性值是可選的,即不是必須的,則忽略該屬性值 if (pv.isOptional()) { logger.debug("Ignoring optional value for property '" + actualName + "' - property not found on bean class [" + getRootClass().getName() + "]"); return; } //如果屬性值是必須的,則拋出無法給屬性賦值,因為每天提供setter方法異常 else { PropertyMatches matches = PropertyMatches.forProperty(propertyName, getRootClass()); throw new NotWritablePropertyException( getRootClass(), this.nestedPath + propertyName, matches.buildErrorMessage(), matches.getPossibleMatches()); } } pv.getOriginalPropertyValue().resolvedDescriptor = pd; } Object oldValue = null; try { Object originalValue = pv.getValue(); Object valueToApply = originalValue; if (!Boolean.FALSE.equals(pv.conversionNecessary)) { if (pv.isConverted()) { valueToApply = pv.getConvertedValue(); } else { if (isExtractOldValueForEditor() && pd.getReadMethod() != null) { //獲取屬性的getter方法(讀方法),JDK內省機制 final Method readMethod = pd.getReadMethod(); //如果屬性的getter方法不是public訪問控制權限的,即訪問控制權限比較嚴格, //則使用JDK的反射機制強行訪問非public的方法(暴力讀取屬性值) if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()) && !readMethod.isAccessible()) { if (System.getSecurityManager()!= null) { //匿名內部類,根據權限修改屬性的讀取控制限制 AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { readMethod.setAccessible(true); return null; } }); } else { readMethod.setAccessible(true); } } try { //屬性沒有提供getter方法時,調用潛在的讀取屬性值//的方法,獲取屬性值 if (System.getSecurityManager() != null) { oldValue = AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { return readMethod.invoke(object); } }, acc); } else { oldValue = readMethod.invoke(object); } } catch (Exception ex) { if (ex instanceof PrivilegedActionException) { ex = ((PrivilegedActionException) ex).getException(); } if (logger.isDebugEnabled()) { logger.debug("Could not read previous value of property '" + this.nestedPath + propertyName + "'", ex); } } } //設置屬性的注入值 valueToApply = convertForProperty(propertyName, oldValue, originalValue, pd); } pv.getOriginalPropertyValue().conversionNecessary = (valueToApply != originalValue); } //根據JDK的內省機制,獲取屬性的setter(寫方法)方法 final Method writeMethod = (pd instanceof GenericTypeAwarePropertyDescriptor ? ((GenericTypeAwarePropertyDescriptor) pd).getWriteMethodForActualAccess() : pd.getWriteMethod()); //如果屬性的setter方法是非public,即訪問控制權限比較嚴格,則使用JDK的反射機制, //強行設置setter方法可訪問(暴力為屬性賦值) if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()) && !writeMethod.isAccessible()) { //如果使用了JDK的安全機制,則需要權限驗證 if (System.getSecurityManager()!= null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { writeMethod.setAccessible(true); return null; } }); } else { writeMethod.setAccessible(true); } } final Object value = valueToApply; if (System.getSecurityManager() != null) { try { //將屬性值設置到屬性上去 AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { writeMethod.invoke(object, value); return null; } }, acc); } catch (PrivilegedActionException ex) { throw ex.getException(); } } else { writeMethod.invoke(this.object, value); } } catch (TypeMismatchException ex) { throw ex; } catch (InvocationTargetException ex) { PropertyChangeEvent propertyChangeEvent = new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); if (ex.getTargetException() instanceof ClassCastException) { throw new TypeMismatchException(propertyChangeEvent, pd.getPropertyType(), ex.getTargetException()); } else { throw new MethodInvocationException(propertyChangeEvent, ex.getTargetException()); } } catch (Exception ex) { PropertyChangeEvent pce = new PropertyChangeEvent(this.rootObject, this.nestedPath + propertyName, oldValue, pv.getValue()); throw new MethodInvocationException(pce, ex); } } }通過對上面注入依賴代碼的分析,我們已經明白了Spring IoC容器是如何將屬性的值注入到Bean實例對象中去的:
(1) 對于集合類型的屬性,將其屬性值解析為目標類型的集合后直接賦值給屬性。
(2) 對于非集合類型的屬性,大量使用了JDK的反射和內省機制,通過屬性的getter方法(reader method)獲取指定屬性注入以前的值,同時調用屬性的setter方法(writer method)為屬性設置注入后的值。看到這里相信很多人都明白了Spring的setter注入原理。
至此Spring IoC容器對Bean定義資源文件的定位,載入、解析和依賴注入已經全部分析完畢,現在Spring IoC容器中管理了一系列靠依賴關系聯系起來的Bean,程序不需要應用自己手動創建所需的對象,Spring IoC容器會在我們使用的時候自動為我們創建,并且為我們注入好相關的依賴,這就是Spring核心功能的控制反轉和依賴注入的相關功能。
容器其他相關特性的設計與實現
Application中Bean的初始化及銷毀
簡要的介紹一下IoC容器中的Bean生命周期:
- Bean實例的創建
- 為Bean實例設置屬性
- 調用Bean的初始化方法
- 應用可以通過IoC容器使用Bean
- 當容器關閉時,調用Bean的銷毀方法
具體來說:
實例化一個Bean--也就是我們常說的new;
按照Spring上下文對實例化的Bean進行配置--也就是IOC注入;
如果這個Bean已經實現了BeanNameAware接口,會調用它實現的setBeanName(String)方法,此處傳遞的就是Spring配置文件中Bean的id值;
如果這個Bean已經實現了BeanFactoryAware接口,會調用它實現的setBeanFactory(setBeanFactory(BeanFactory)傳遞的是Spring工廠自身(可以用這個方式來獲取其它Bean,只需在Spring配置文件中配置一個普通的Bean就可以);
如果這個Bean已經實現了ApplicationContextAware接口,會調用setApplicationContext(ApplicationContext)方法,傳入Spring上下文(同樣這個方式也可以實現步驟4的內容,但比4更好,因為ApplicationContext是BeanFactory的子接口,有更多的實現方法);
如果這個Bean關聯了BeanPostProcessor接口,將會調用postProcessBeforeInitialization(Object obj, String s)方法,BeanPostProcessor經常被用作是Bean內容的更改,并且由于這個是在Bean初始化結束時調用那個的方法,也可以被應用于內存或緩存技術;
如果bean實現了InitializingBean接口,spring將調用它的afterPropertiesSet接口方法;如果Bean在Spring配置文件中配置了init-method屬性會自動調用其配置的初始化方法。
如果這個Bean關聯了BeanPostProcessor接口,將會調用postProcessAfterInitialization(Object obj, String s)方法、;
注:以上工作完成以后就可以應用這個Bean了,那這個Bean是一個Singleton的,所以一般情況下我們調用同一個id的Bean會是在內容地址相同的實例,當然在Spring配置文件中也可以配置非Singleton,這里我們不做贅述。
當Bean不再需要時,會經過清理階段,如果Bean實現了DisposableBean這個接口,會調用那個其實現的destroy()方法;
最后,如果這個Bean的Spring配置中配置了destroy-method屬性,會自動調用其配置的銷毀方法。
注:以上10步驟可以作為面試或者筆試的模板,另外我們這里描述的是應用Spring上下文Bean的生命周期,如果應用Spring的工廠也就是BeanFactory的話去掉第5步就Ok了。
lazy-init屬性和預實例化
IoC容器的初始化過程就是對Bean定義資源的定位、載入和注冊,此時容器對Bean的依賴注入并沒有發生,依賴注入主要是在應用程序第一次向容器索取Bean時,通過getBean方法的調用完成。 當Bean定義資源的< Bean >元素中配置了lazy-init屬性時,容器將會在初始化的時候對所配置的Bean進行預實例化,Bean的依賴注入在容器初始化的時候就已經完成。這樣,當應用程序第一次向容器索取被管理的Bean時,就不用再初始化和對Bean進行依賴注入了,直接從容器中獲取已經完成依賴注入的現成Bean,可以提高應用第一次向容器獲取Bean的性能。 當Bean定義資源被載入IoC容器之后,容器將Bean定義資源解析為容器內部的數據結構BeanDefinition注冊到容器中,AbstractApplicationContext類中的finishBeanFactoryInitialization方法對配置了預實例化屬性的Bean進行預初始化過程。
FactoryBean的實現
FactoryBean是一個典型的工廠模式的使用。它是一個Bean,作用是產生其他bean實例。通常情況下,僅需要提供一個工廠方法,該方法用來返回其他Bean實例。通常情況下,bean無須自己實現工廠模式,Spring容器擔任工廠角色;但少數情況下,容器中的bean本身就是工廠,其作用是產生其它Bean實例。
當用戶使用容器本身時,可以使用轉義字符”&”來得到FactoryBean本身,以區別通過FactoryBean產生的實例對象和FactoryBean對象本身。在BeanFactory中通過如下代碼定義了該轉義字符。
BeanPostProcessor的實現
BeanPostProcessor后置處理器是Spring IoC容器經常使用到的一個特性,這個Bean后置處理器是一個監聽器,可以監聽容器觸發的Bean聲明周期事件。后置處理器向容器注冊以后,容器中管理的Bean就具備了接收IoC容器事件回調的能力。 BeanPostProcessor的使用非常簡單,只需要提供一個實現接口BeanPostProcessor的實現類,然后在Bean的配置文件中設置即可。
package org.springframework.beans.factory.config; import org.springframework.beans.BeansException; public interface BeanPostProcessor { //為在Bean的初始化前提供回調入口 Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; //為在Bean的初始化之后提供回調入口 Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; }這兩個回調的入口都是和容器管理的Bean的生命周期事件緊密相關,可以為用戶提供在Spring IoC容器初始化Bean過程中自定義的處理操作。
autowiring(自動裝配的實現)
在自動裝配中,Spring IoC容器的依賴自動裝配功能,不需要對Bean屬性的依賴關系做顯式的聲明,只需要在配置好autowiring屬性,IoC容器會自動使用反射查找屬性的類型和名稱,然后基于屬性的類型或者名稱來自動匹配容器中管理的Bean,從而自動地完成依賴注入。
應用第一次通過getBean方法(配置了lazy-init預實例化屬性的除外)向IoC容器索取Bean時,容器創建Bean實例對象,并且對Bean實例對象進行屬性依賴注入,AbstractAutoWireCapableBeanFactory的populateBean方法就是實現Bean屬性依賴注入的功能。
在populateBean的實現中,在處理一般的Bean之前,先對autowiring屬性進行處理。如果當前的Bean配置了autowire_by_name和autowire_by_type屬性,那么調用相應的autowireByName方法autowireByType方法。對于autowire_by_name,它首先通過反射機制從當前Bean中得到需要注入的屬性名,然后使用這個屬性名向容器申請與之同名的Bean,這樣實際又觸發了另一個Bean的生成和依賴注入的過程。
autowiring的實現過程:
a. 對Bean的屬性迭代調用getBean方法,完成依賴Bean的初始化和依賴注入。
b. 將依賴Bean的屬性引用設置到被依賴的Bean屬性上。
c. 將依賴Bean的名稱和被依賴的Bean的名稱存儲在IoC容器的集合中。
Bean的依賴檢查
通過依賴檢查特性,Spring可以幫助應用檢查是否所有的屬性已經被正確設置。在具體使用的時候,應用只需要在Bean定義中設置dependency-check屬性來指定依賴檢查模式即可。
這里可以將屬性設置為none、simple、object、all四種模式,默認的模式是none。
總結
以上是生活随笔為你收集整理的Spring IoC容器设计原理及高级特性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度 orc识别图片转文字
- 下一篇: MSSQL2005的新功能创建数据库快照