javascript
Spring IoC容器设计原理及高级特性
文章目錄
- Spring IoC容器概述
- IoC容器系列的設(shè)計(jì)與實(shí)現(xiàn):BeanFactory和ApplicationContext
- BeanFactory
- BeanFactory容器的設(shè)計(jì)原理
- ApplicationContext
- ApplicationContext容器的設(shè)計(jì)原理
- IoC容器的初始化過程
- IoC容器的依賴注入
- 依賴注入發(fā)生的時(shí)間
- 依賴注入的流程圖
- 依賴注入的源碼分析
- 容器其他相關(guān)特性的設(shè)計(jì)與實(shí)現(xiàn)
- Application中Bean的初始化及銷毀
- lazy-init屬性和預(yù)實(shí)例化
- FactoryBean的實(shí)現(xiàn)
- BeanPostProcessor的實(shí)現(xiàn)
- autowiring(自動(dòng)裝配的實(shí)現(xiàn))
- Bean的依賴檢查
Spring IoC容器概述
IoC是Spring容器的內(nèi)核,其字面意思是控制反轉(zhuǎn)。那么究竟是哪些方面的控制被反轉(zhuǎn)了?其實(shí)是依賴對(duì)象的獲得被反轉(zhuǎn)了。基于此,提出了DI(依賴注入)的概念。
IoC容器系列的設(shè)計(jì)與實(shí)現(xiàn):BeanFactory和ApplicationContext
在Spring IoC容器的設(shè)計(jì)中,主要有兩個(gè)容器系列,一個(gè)是實(shí)現(xiàn)BeanFactory接口餓簡(jiǎn)單容器系列,這系列容器只實(shí)現(xiàn)了容器的最基本功能;另一個(gè)是ApplicationContext應(yīng)用上下文,它作為容器的高級(jí)形態(tài)而存在。應(yīng)用上下文在簡(jiǎn)單容器的基礎(chǔ)上,增加了許多面向框架的特性,同時(shí)對(duì)應(yīng)用環(huán)境做了許多適配。
Spring通過定義BeanDefinition來管理基于Spring的應(yīng)用中的各種對(duì)象以及它們之間的相互依賴關(guān)系。對(duì)IoC容器來說,BeanDefinition就是依賴反轉(zhuǎn)模式中管理的對(duì)象依賴關(guān)系的數(shù)據(jù)抽象,也是容器實(shí)現(xiàn)依賴反轉(zhuǎn)功能的核心數(shù)據(jù)結(jié)構(gòu),依賴反轉(zhuǎn)功能都是圍繞這個(gè)BeanDefinition的處理來完成的。
? 從接口BeanFactory到HierarchicalBeanFactory,再到ConfigurableBeanFactory,是一條主要的BeanFactory設(shè)計(jì)路徑
BeanFactory定義了基本的IoC容器的規(guī)范。包括了getBean()(通過這個(gè)方法可以從容器中取得Bean)。
HierarchicalBeanFactory接口在繼承了BeanFactory后,增加了getParentBeanFactory(),使BeanFactory具備了雙親IoC容器的管理功能。
在接下來的ConfigurableBeanFactory中,定義了一些對(duì)BeanFactory的配置功能,比如通過setParentBeanFactory()設(shè)置雙親IoC容器,通過addBeanPostProcessor()配置Bean后置處理器,等等。
? 第二條接口設(shè)計(jì)主線是,以ApplicationContext為核心的接口設(shè)計(jì)
我們常用的應(yīng)用上下文基本上都是ConfigurableApplicationContext或者WebApplicationContext的實(shí)現(xiàn)
在這個(gè)接口體系中,ListableBeanFactory和HierarchicalBeanFactory兩個(gè)接口,連接BeanFactory接口定義和ApplicationConext應(yīng)用上下文的接口定義。
在ListableBeanFactory接口中,細(xì)化了許多BeanFactory的接口功能,比如定義了getBeanDefinitionNames()接口方法;對(duì)于HierarchicalBeanFactory接口,我們?cè)谇拔闹幸呀?jīng)提到過;對(duì)于ApplicationContext接口,它通過繼承MessageSource、ResourceLoader、ApplicationEventPublisher接口,在BeanFactory簡(jiǎn)單IoC容器的基礎(chǔ)上添加了許多對(duì)高級(jí)容器的特性的支持
? 這里涉及的是主要接口關(guān)系,而具體的IoC容器都是在這個(gè)接口體系下實(shí)現(xiàn)的,比如DefaultListableBeanFactory,這個(gè)基本IoC容器的實(shí)現(xiàn)就是實(shí)現(xiàn)了ConfigurableBeanFactory,從而成為一個(gè)簡(jiǎn)單IoC容器的實(shí)現(xiàn)。
像其他IoC容器,比如XmlBeanFactory,都是在DefaultListableBeanFactory的基礎(chǔ)上做擴(kuò)展
? 這個(gè)接口系統(tǒng)是以BeanFactory和ApplicationContext為核心
而BeanFactory又是IoC容器的最基本接口,在ApplicationContext的設(shè)計(jì)中,一方面,可以看到它繼承了BeanFactory接口體系的接口,具備了BeanFactory IoC容器的基本功能
另一方面,通過繼承MessageSource、ResourceLoadr、ApplicationEventPublisher這些接口,BeanFactory為ApplicationContext賦予了更高級(jí)的IoC容器特性。
對(duì)于ApplicationContext而言,為了在Web環(huán)境中使用它,還設(shè)計(jì)了WebApplicationContext接口,而這個(gè)接口通過繼承ThemeSource接口來擴(kuò)充功能。
BeanFactory
Spring Bean的創(chuàng)建是典型的工廠模式,這一系列的Bean工廠,也即IOC容器為開發(fā)者管理對(duì)象間的依賴關(guān)系提供了很多便利和基礎(chǔ)服務(wù),在Spring中有許多的IOC容器的實(shí)現(xiàn)供用戶選擇和使用,其相互關(guān)系如下:
BeanFactory接口定義了IoC容器最基本的形式,不關(guān)心 Bean 是怎樣定義和加載的。如果我們想要知道一個(gè)工廠具體產(chǎn)生對(duì)象的過程,則要看這個(gè)接口的實(shí)現(xiàn)類。
其中BeanFactory作為最頂層的一個(gè)接口類,它定義了IOC容器的基本功能規(guī)范,BeanFactory 有三個(gè)子類:
ListableBeanFactory、HierarchicalBeanFactory 和AutowireCapableBeanFactory。
但是從上圖中我們可以發(fā)現(xiàn)最終的默認(rèn)實(shí)現(xiàn)類是 DefaultListableBeanFactory,他實(shí)現(xiàn)了所有的接口。那為何要定義這么多層次的接口呢?查閱這些接口的源碼和說明發(fā)現(xiàn),每個(gè)接口都有他使用的場(chǎng)合,它主要是為了區(qū)分在 Spring 內(nèi)部在操作過程中對(duì)象的傳遞和轉(zhuǎn)化過程中,對(duì)對(duì)象的數(shù)據(jù)訪問所做的限制。例如 ListableBeanFactory 接口表示這些 Bean 是可列表的,而 HierarchicalBeanFactory 表示子容器可以通過接口方法訪問父容器,AutowireCapableBeanFactory 接口定義 Bean 的自動(dòng)裝配規(guī)則。這四個(gè)接口共同定義了 Bean 的集合、Bean 之間的關(guān)系、以及 Bean 行為。
最基本的IOC容器接口BeanFactory
public interface BeanFactory { //對(duì)FactoryBean的轉(zhuǎn)義定義,因?yàn)槿绻褂胋ean的名字檢索FactoryBean得到的對(duì)象是工廠生成的對(duì)象, //如果需要得到工廠本身,需要轉(zhuǎn)義 String FACTORY_BEAN_PREFIX = "&"; //根據(jù)bean的名字,獲取在IOC容器中得到bean實(shí)例 Object getBean(String name) throws BeansException; //根據(jù)bean的名字和Class類型來得到bean實(shí)例,增加了類型安全驗(yàn)證機(jī)制。 Object getBean(String name, Class requiredType) throws BeansException; //提供對(duì)bean的檢索,看看是否在IOC容器有這個(gè)名字的bean boolean containsBean(String name); //根據(jù)bean名字得到bean實(shí)例,并同時(shí)判斷這個(gè)bean是不是單例 boolean isSingleton(String name) throws NoSuchBeanDefinitionException; //得到bean實(shí)例的Class類型 Class getType(String name) throws NoSuchBeanDefinitionException; //得到bean的別名,如果根據(jù)別名檢索,那么其原名也會(huì)被檢索出來 String[] getAliases(String name); }BeanFactory容器的設(shè)計(jì)原理
在BeanFactory接口的基礎(chǔ)上,Spring提供了一系列容器的實(shí)現(xiàn)供開發(fā)人員使用。我們以XmlBeanFactory的實(shí)現(xiàn)為例來說明簡(jiǎn)單IoC容器的設(shè)計(jì)原理。
XmlBeanFactory繼承自DefaultListableBeanFactory這個(gè)類,同時(shí)實(shí)現(xiàn)了其他諸如XML讀取的附加功能。即它是一個(gè)可以讀取以XML文件方式定義的BeanDefinition的IoC容器。
在XmlBeanFactory中,初始化了一個(gè)XmlBeanDefinitionReader對(duì)象,由它來完成XML形式的信息處理。構(gòu)造XmlBeanFactory這個(gè)容器時(shí),需要指定BeanDefinition的信息來源,將它封裝成Spring中的Resource類來給出。然后傳遞給XmlBeanFactory構(gòu)造函數(shù),IoC容器就可以方便地定位到需要的BeanDefinition信息來對(duì)Bean完成容器的初始化和依賴注入過程。對(duì)XmlBeanDefinitionReader對(duì)象的初始化,以及使用這個(gè)對(duì)象來完成對(duì)loadBeanDefinition的調(diào)用,就是這個(gè)調(diào)用啟動(dòng)從Resource中載入BeanDefinitions的過程。
ApplicationContext
ApplicationContext是Spring提供的一個(gè)高級(jí)的IoC容器,它除了能夠提供IoC容器的基本功能外,還為用戶提供了以下的附加服務(wù)。
ApplicationContext容器的設(shè)計(jì)原理
我們以常用的FileSystemXmlApplicationContext的實(shí)現(xiàn)為例說明ApplicationContext容器的設(shè)計(jì)原理。
public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException { super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); } }在FileSystemXmlApplicationContext的設(shè)計(jì)中,我們看到ApplicationContext應(yīng)用上下文的主要功能已經(jīng)在FileSystemXmlApplicationContext的基類AbstractXmlApplicationContext中實(shí)現(xiàn)了,FileSystemXmlApplicationContext作為一個(gè)具體的應(yīng)用上下文,只需要實(shí)現(xiàn)和它自身設(shè)計(jì)相關(guān)的兩個(gè)功能。一個(gè)功能是,如果應(yīng)用直接使用FileSystemXmlApplicationContext,對(duì)于實(shí)例化這個(gè)應(yīng)用上下文的支持,同時(shí)啟動(dòng)IoC容器的refresh()過程。另一個(gè)功能是與FileSystemXmlApplicationContext設(shè)計(jì)具體相關(guān)的功能,這部分與怎樣從文件系統(tǒng)中加載XML的Bean定義資源有關(guān)。
IoC容器的初始化過程
IoC容器的初始化是由refresh()方法來啟動(dòng)的,這個(gè)方法標(biāo)志著容器的正式啟動(dòng)。具體來說,這個(gè)啟動(dòng)包括BeanDefinition的Resource定位、載入和注冊(cè)三個(gè)基本過程。
- Resource定位:指的是BeanDefinition的資源定位,它由ResourceLoader通過統(tǒng)一的Resource接口來完成。
- BeanDefinition的載入:把用戶定義好的Bea表示成IoC容器內(nèi)部的數(shù)據(jù)結(jié)構(gòu),而這個(gè)容器內(nèi)部的數(shù)據(jù)結(jié)構(gòu)就是BeanDefinition。
- BeanDefinition的注冊(cè):通過調(diào)用BeanDefinitionRegistry接口的實(shí)現(xiàn)來完成的。這個(gè)注冊(cè)過程是把載入過程中解析得到的BeanDefinition向IoC容器進(jìn)行注冊(cè)。
IoC容器的依賴注入
依賴注入發(fā)生的時(shí)間
當(dāng)Spring IoC容器完成了Bean定義資源的定位、載入和解析注冊(cè)以后,IoC容器中已經(jīng)管理類Bean定義的相關(guān)數(shù)據(jù),即初始化過程完成的主要工作是在IoC容器中建立BeanDefinition數(shù)據(jù)映射。
但是此時(shí)IoC容器還沒有對(duì)所管理的Bean進(jìn)行依賴注入,依賴注入在以下兩種情況發(fā)生:
(1) 用戶第一次通過getBean方法向IoC容索要Bean時(shí),IoC容器觸發(fā)依賴注入。
(2) 當(dāng)用戶在Bean定義資源中為< Bean >元素配置了lazy-init屬性,即讓容器在解析注冊(cè)Bean定義時(shí)進(jìn)行預(yù)實(shí)例化,觸發(fā)依賴注入。
BeanFactory接口定義了Spring IoC容器的基本功能規(guī)范,是Spring IoC容器所應(yīng)遵守的最底層和最基本的編程規(guī)范。BeanFactory接口中定義了幾個(gè)getBean方法,就是用戶向IoC容器索取管理的Bean的方法,我們通過分析其子類的具體實(shí)現(xiàn),理解Spring IoC容器在用戶索取Bean時(shí)如何完成依賴注入。
依賴注入的流程圖
依賴注入的源碼分析
在BeanFactory中我們看到getBean()函數(shù),它的具體實(shí)現(xiàn)在AbstractBeanFactory中。
AbstractBeanFactory的getBean相關(guān)方法的源碼如下:
//獲取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容器中指定名稱和參數(shù)的Bean public Object getBean(String name, Object... args) throws BeansException { //doGetBean才是真正向IoC容器獲取被管理Bean的過程 return doGetBean(name, null, args, false); } //獲取IoC容器中指定名稱、類型和參數(shù)的Bean public <T> T getBean(String name, Class<T> requiredType, Object... args) throws BeansException { //doGetBean才是真正向IoC容器獲取被管理Bean的過程 return doGetBean(name, requiredType, args, false); } //真正實(shí)現(xiàn)向IoC容器獲取Bean的功能,也是觸發(fā)依賴注入功能的地方 @SuppressWarnings("unchecked") protected <T> T doGetBean( final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { //根據(jù)指定的名稱獲取被管理Bean的名稱,剝離指定名稱中對(duì)容器的相關(guān)依賴 //如果指定的是別名,將別名轉(zhuǎn)換為規(guī)范的Bean名稱 final String beanName = transformedBeanName(name); Object bean; //先從緩存中取是否已經(jīng)有被創(chuàng)建過的單例類型的Bean,對(duì)于單例模式的Bean整 //個(gè)IoC容器中只創(chuàng)建一次,不需要重復(fù)創(chuàng)建 Object sharedInstance = getSingleton(beanName); //IoC容器創(chuàng)建單例模式Bean實(shí)例對(duì)象 if (sharedInstance != null && args == null) { if (logger.isDebugEnabled()) { //如果指定名稱的Bean在容器中已有單例模式的Bean被創(chuàng)建,直接返回 //已經(jīng)創(chuàng)建的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的實(shí)例對(duì)象,主要是完成FactoryBean的相關(guān)處理 //注意:BeanFactory是管理容器中Bean的工廠,而FactoryBean是 //創(chuàng)建創(chuàng)建對(duì)象的工廠Bean,兩者之間有區(qū)別 bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else {//緩存沒有正在創(chuàng)建的單例模式Bean //緩存中已經(jīng)有已經(jīng)創(chuàng)建的原型模式Bean,但是由于循環(huán)引用的問題導(dǎo)致實(shí) //例化對(duì)象失敗 if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } //對(duì)IoC容器中是否存在指定名稱的BeanDefinition進(jìn)行檢查,首先檢查是否 //能在當(dāng)前的BeanFactory中獲取的所需要的Bean,如果不能則委托當(dāng)前容器 //的父級(jí)容器去查找,如果還是找不到則沿著容器的繼承體系向父級(jí)容器查找 BeanFactory parentBeanFactory = getParentBeanFactory(); //當(dāng)前容器的父級(jí)容器存在,且當(dāng)前容器中不存在指定名稱的Bean if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { //解析指定Bean名稱的原始名稱 String nameToLookup = originalBeanName(name); if (args != null) { //委派父級(jí)容器根據(jù)指定名稱和顯式的參數(shù)查找 return (T) parentBeanFactory.getBean(nameToLookup, args); } else { //委派父級(jí)容器根據(jù)指定名稱和類型查找 return parentBeanFactory.getBean(nameToLookup, requiredType); } } //創(chuàng)建的Bean是否需要進(jìn)行類型驗(yàn)證,一般不需要 if (!typeCheckOnly) { //向容器標(biāo)記指定的Bean已經(jīng)被創(chuàng)建 markBeanAsCreated(beanName); } //根據(jù)指定Bean名稱獲取其父級(jí)的Bean定義,主要解決Bean繼承時(shí)子類 //合并父類公共屬性問題 final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); //獲取當(dāng)前Bean所有依賴Bean的名稱 String[] dependsOn = mbd.getDependsOn(); //如果當(dāng)前Bean有依賴Bean if (dependsOn != null) { for (String dependsOnBean : dependsOn) { //遞歸調(diào)用getBean方法,獲取當(dāng)前Bean的依賴Bean getBean(dependsOnBean); //把被依賴Bean注冊(cè)給當(dāng)前依賴的Bean registerDependentBean(dependsOnBean, beanName); } } //創(chuàng)建單例模式Bean的實(shí)例對(duì)象 if (mbd.isSingleton()) { //這里使用了一個(gè)匿名內(nèi)部類,創(chuàng)建Bean實(shí)例對(duì)象,并且注冊(cè)給所依賴的對(duì)象 sharedInstance = getSingleton(beanName, new ObjectFactory() { public Object getObject() throws BeansException { try { //創(chuàng)建一個(gè)指定Bean實(shí)例對(duì)象,如果有父級(jí)繼承,則合并子//類和父類的定義 return createBean(beanName, mbd, args); } catch (BeansException ex) { //顯式地從容器單例模式Bean緩存中清除實(shí)例對(duì)象 destroySingleton(beanName); throw ex; } } }); //獲取給定Bean的實(shí)例對(duì)象 bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } //IoC容器創(chuàng)建原型模式Bean實(shí)例對(duì)象 else if (mbd.isPrototype()) { //原型模式(Prototype)是每次都會(huì)創(chuàng)建一個(gè)新的對(duì)象 Object prototypeInstance = null; try { //回調(diào)beforePrototypeCreation方法,默認(rèn)的功能是注冊(cè)當(dāng)前創(chuàng)//建的原型對(duì)象 beforePrototypeCreation(beanName); //創(chuàng)建指定Bean對(duì)象實(shí)例 prototypeInstance = createBean(beanName, mbd, args); } finally { //回調(diào)afterPrototypeCreation方法,默認(rèn)的功能告訴IoC容器指//定Bean的原型對(duì)象不再創(chuàng)建了 afterPrototypeCreation(beanName); } //獲取給定Bean的實(shí)例對(duì)象 bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } //要?jiǎng)?chuàng)建的Bean既不是單例模式,也不是原型模式,則根據(jù)Bean定義資源中 //配置的生命周期范圍,選擇實(shí)例化Bean的合適方法,這種在Web應(yīng)用程序中 //比較常用,如: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 { //這里又使用了一個(gè)匿名內(nèi)部類,獲取一個(gè)指定生命周期范圍的實(shí)例 Object scopedInstance = scope.get(beanName, new ObjectFactory() { public Object getObject() throws BeansException { beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } } }); //獲取給定Bean的實(shí)例對(duì)象 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); } } } //對(duì)創(chuàng)建的Bean實(shí)例對(duì)象進(jìn)行類型檢查 if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) { throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } return (T) bean; }通過上面對(duì)向IoC容器獲取Bean方法的分析,我們可以看到在Spring中,如果Bean定義的單例模式(Singleton),則容器在創(chuàng)建之前先從緩存中查找,以確保整個(gè)容器中只存在一個(gè)實(shí)例對(duì)象。如果Bean定義的是原型模式(Prototype),則容器每次都會(huì)創(chuàng)建一個(gè)新的實(shí)例對(duì)象。除此之外,Bean定義還可以擴(kuò)展為指定其生命周期范圍。
上面的源碼只是定義了根據(jù)Bean定義的模式,采取的不同創(chuàng)建Bean實(shí)例對(duì)象的策略,具體的Bean實(shí)例對(duì)象的創(chuàng)建過程由實(shí)現(xiàn)了ObejctFactory接口的匿名內(nèi)部類的createBean方法完成,ObejctFactory使用委派模式,具體的Bean實(shí)例創(chuàng)建過程交由其實(shí)現(xiàn)類AbstractAutowireCapableBeanFactory完成,我們繼續(xù)分析AbstractAutowireCapableBeanFactory的createBean方法的源碼,理解其創(chuàng)建Bean實(shí)例的具體實(shí)現(xiàn)過程。
AbstractAutowireCapableBeanFactory類實(shí)現(xiàn)了ObejctFactory接口,創(chuàng)建容器指定的Bean實(shí)例對(duì)象,同時(shí)還對(duì)創(chuàng)建的Bean實(shí)例對(duì)象進(jìn)行初始化處理。其創(chuàng)建Bean實(shí)例對(duì)象的方法源碼如下:
//創(chuàng)建Bean實(shí)例對(duì)象 protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) throws BeanCreationException { if (logger.isDebugEnabled()) { logger.debug("Creating instance of bean '" + beanName + "'"); } //判斷需要?jiǎng)?chuàng)建的Bean是否可以實(shí)例化,即是否可以通過當(dāng)前的類加載器加載 resolveBeanClass(mbd, beanName); //校驗(yàn)和準(zhǔn)備Bean中的方法覆蓋 try { mbd.prepareMethodOverrides(); } catch (BeanDefinitionValidationException ex) { throw new BeanDefinitionStoreException(mbd.getResourceDescription(), beanName, "Validation of method overrides failed", ex); } try { //如果Bean配置了初始化前和初始化后的處理器,則試圖返回一個(gè)需要?jiǎng)?chuàng)建//Bean的代理對(duì)象 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); } //創(chuàng)建Bean的入口 Object beanInstance = doCreateBean(beanName, mbd, args); if (logger.isDebugEnabled()) { logger.debug("Finished creating instance of bean '" + beanName + "'"); } return beanInstance; } //真正創(chuàng)建Bean的方法 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) { //封裝被創(chuàng)建的Bean對(duì)象 BeanWrapper instanceWrapper = null; if (mbd.isSingleton()){//單例模式的Bean,先從容器中緩存中獲取同名Bean instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); } if (instanceWrapper == null) { //創(chuàng)建實(shí)例對(duì)象 instanceWrapper = createBeanInstance(beanName, mbd, args); } final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null); //獲取實(shí)例化對(duì)象的類型 Class beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null); //調(diào)用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對(duì)象,以防循環(huán)引用 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"); } //這里是一個(gè)匿名內(nèi)部類,為了防止循環(huán)引用,盡早持有對(duì)象的引用 addSingletonFactory(beanName, new ObjectFactory() { public Object getObject() throws BeansException { return getEarlyBeanReference(beanName, mbd, bean); } }); } //Bean對(duì)象的初始化,依賴注入在此觸發(fā) //這個(gè)exposedObject在初始化完成之后返回作為依賴注入完成后的Bean Object exposedObject = bean; try { //將Bean實(shí)例對(duì)象封裝,并且Bean定義中配置的屬性值賦值給實(shí)例對(duì)象 populateBean(beanName, mbd, instanceWrapper); if (exposedObject != null) { //初始化Bean對(duì)象 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) { //獲取指定名稱的已注冊(cè)的單例模式Bean對(duì)象 Object earlySingletonReference = getSingleton(beanName, false); if (earlySingletonReference != null) { //根據(jù)名稱獲取的以注冊(cè)的Bean和正在實(shí)例化的Bean是同一個(gè) if (exposedObject == bean) { //當(dāng)前實(shí)例化的Bean初始化完成 exposedObject = earlySingletonReference; } //當(dāng)前Bean依賴其他Bean,并且當(dāng)發(fā)生循環(huán)引用時(shí)不允許新創(chuàng)建實(shí)例對(duì)象 else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) { String[] dependentBeans = getDependentBeans(beanName); Set<String> actualDependentBeans = new LinkedHashSet<String>(dependentBeans.length); //獲取當(dāng)前Bean所依賴的其他Bean for (String dependentBean : dependentBeans) { //對(duì)依賴Bean進(jìn)行類型檢查 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."); } } } } //注冊(cè)完成依賴注入的Bean try { registerDisposableBeanIfNecessary(beanName, bean, mbd); } catch (BeanDefinitionValidationException ex) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex); } return exposedObject; }通過對(duì)方法源碼的分析,我們看到具體的依賴注入實(shí)現(xiàn)在以下兩個(gè)方法中:
(1) createBeanInstance:生成Bean所包含的java對(duì)象實(shí)例。
(2) populateBean :對(duì)Bean屬性的依賴注入進(jìn)行處理。
下面繼續(xù)分析這兩個(gè)方法的代碼實(shí)現(xiàn)。
在createBeanInstance方法中,根據(jù)指定的初始化策略,使用靜態(tài)工廠、工廠方法或者容器的自動(dòng)裝配特性生成java實(shí)例對(duì)象,創(chuàng)建對(duì)象的源碼如下:
//創(chuàng)建Bean的實(shí)例對(duì)象 protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, Object[] args) { //檢查確認(rèn)Bean是可實(shí)例化的 Class beanClass = resolveBeanClass(mbd, beanName); //使用工廠方法對(duì)Bean進(jìn)行實(shí)例化 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) { //調(diào)用工廠方法實(shí)例化 return instantiateUsingFactoryMethod(beanName, mbd, args); } //使用容器的自動(dòng)裝配方法進(jìn)行實(shí)例化 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) { //配置了自動(dòng)裝配屬性,使用容器的自動(dòng)裝配實(shí)例化 //容器的自動(dòng)裝配是根據(jù)參數(shù)類型匹配Bean的構(gòu)造方法 return autowireConstructor(beanName, mbd, null, null); } else { //使用默認(rèn)的無參構(gòu)造方法實(shí)例化 return instantiateBean(beanName, mbd); } } //使用Bean的構(gòu)造方法進(jìn)行實(shí)例化 Constructor[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName); if (ctors != null || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR || mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) { //使用容器的自動(dòng)裝配特性,調(diào)用匹配的構(gòu)造方法實(shí)例化 return autowireConstructor(beanName, mbd, ctors, args); } //使用默認(rèn)的無參構(gòu)造方法實(shí)例化 return instantiateBean(beanName, mbd); } //使用默認(rèn)的無參構(gòu)造方法實(shí)例化Bean對(duì)象 protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) { try { Object beanInstance; final BeanFactory parent = this; //獲取系統(tǒng)的安全管理接口,JDK標(biāo)準(zhǔn)的安全管理API if (System.getSecurityManager() != null) { //這里是一個(gè)匿名內(nèi)置類,根據(jù)實(shí)例化策略創(chuàng)建實(shí)例對(duì)象 beanInstance = AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { return getInstantiationStrategy().instantiate(mbd, beanName, parent); } }, getAccessControlContext()); } else { //將實(shí)例化的對(duì)象封裝起來 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); } }經(jīng)過對(duì)上面的代碼分析,我們可以看出,對(duì)使用工廠方法和自動(dòng)裝配特性的Bean的實(shí)例化相對(duì)比較清楚,調(diào)用相應(yīng)的工廠方法或者參數(shù)匹配的構(gòu)造方法即可完成實(shí)例化對(duì)象的工作,但是對(duì)于我們最常使用的默認(rèn)無參構(gòu)造方法就需要使用相應(yīng)的初始化策略(JDK的反射機(jī)制或者CGLIB)來進(jìn)行初始化了,在方法getInstantiationStrategy().instantiate中就具體實(shí)現(xiàn)類使用初始策略實(shí)例化對(duì)象。
在使用默認(rèn)的無參構(gòu)造方法創(chuàng)建Bean的實(shí)例化對(duì)象時(shí),方法getInstantiationStrategy().instantiate調(diào)用了SimpleInstantiationStrategy類中的實(shí)例化Bean的方法,其源碼如下:
//使用初始化策略實(shí)例化Bean對(duì)象 public Object instantiate(RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) { //如果Bean定義中沒有方法覆蓋,則就不需要CGLIB父類類的方法 if (beanDefinition.getMethodOverrides().isEmpty()) { Constructor<?> constructorToUse; synchronized (beanDefinition.constructorArgumentLock) { //獲取對(duì)象的構(gòu)造方法或工廠方法 constructorToUse = (Constructor<?>) beanDefinition.resolvedConstructorOrFactoryMethod; //如果沒有構(gòu)造方法且沒有工廠方法 if (constructorToUse == null) { //使用JDK的反射機(jī)制,判斷要實(shí)例化的Bean是否是接口 final Class clazz = beanDefinition.getBeanClass(); if (clazz.isInterface()) { throw new BeanInstantiationException(clazz, "Specified class is an interface"); } try { if (System.getSecurityManager() != null) { //這里是一個(gè)匿名內(nèi)置類,使用反射機(jī)制獲取Bean的構(gòu)造方法 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實(shí)例化,通過反射機(jī)制調(diào)用”構(gòu)造方法.newInstance(arg)”來進(jìn)行實(shí)例化 return BeanUtils.instantiateClass(constructorToUse); } else { //使用CGLIB來實(shí)例化對(duì)象 return instantiateWithMethodInjection(beanDefinition, beanName, owner); } }通過上面的代碼分析,我們看到了如果Bean有方法被覆蓋了,則使用JDK的反射機(jī)制進(jìn)行實(shí)例化,否則,使用CGLIB進(jìn)行實(shí)例化。
instantiateWithMethodInjection方法調(diào)用SimpleInstantiationStrategy的子類CglibSubclassingInstantiationStrategy使用CGLIB來進(jìn)行初始化,其源碼如下:
//使用CGLIB進(jìn)行Bean對(duì)象實(shí)例化 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方法生成實(shí)例對(duì)象 return (ctor == null) ? enhancer.create() : enhancer.create(ctor.getParameterTypes(), args); }CGLIB是一個(gè)常用的字節(jié)碼生成器的類庫(kù),它提供了一系列API實(shí)現(xiàn)java字節(jié)碼的生成和轉(zhuǎn)換功能。我們?cè)趯W(xué)習(xí)JDK的動(dòng)態(tài)代理時(shí)都知道,JDK的動(dòng)態(tài)代理只能針對(duì)接口,如果一個(gè)類沒有實(shí)現(xiàn)任何接口,要對(duì)其進(jìn)行動(dòng)態(tài)代理只能使用CGLIB。
在第2步的分析中我們已經(jīng)了解到Bean的依賴注入分為以下兩個(gè)過程:
(1) createBeanInstance:生成Bean所包含的java對(duì)象實(shí)例。
(2) populateBean :對(duì)Bean屬性的依賴注入進(jìn)行處理。
第3、4步中我們已經(jīng)分析了容器初始化生成Bean所包含的Java實(shí)例對(duì)象的過程,現(xiàn)在我們繼續(xù)分析生成對(duì)象后,Spring IoC容器是如何將Bean的屬性依賴關(guān)系注入Bean實(shí)例對(duì)象中并設(shè)置好的,屬性依賴注入的代碼如下:
//將Bean屬性設(shè)置到生成的實(shí)例對(duì)象上 protected void populateBean(String beanName, AbstractBeanDefinition mbd, BeanWrapper bw) { //獲取容器在解析Bean定義資源時(shí)為BeanDefiniton中設(shè)置的屬性值 PropertyValues pvs = mbd.getPropertyValues(); //實(shí)例對(duì)象為null if (bw == null) { //屬性值不為空 if (!pvs.isEmpty()) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance"); } else { //實(shí)例對(duì)象為null,屬性值也為空,不需要設(shè)置屬性值,直接返回 return; } } //在設(shè)置屬性之前調(diào)用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自動(dòng)裝配的注入 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME || mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs = new MutablePropertyValues(pvs); //對(duì)autowire自動(dòng)裝配的處理,根據(jù)Bean名稱自動(dòng)裝配注入 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) { autowireByName(beanName, mbd, bw, newPvs); } //根據(jù)Bean類型自動(dòng)裝配注入 if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) { autowireByType(beanName, mbd, bw, newPvs); } pvs = newPvs; } //檢查容器是否持有用于處理單態(tài)模式Bean關(guān)閉時(shí)的后置處理器 boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors(); //Bean實(shí)例對(duì)象沒有依賴,即沒有繼承基類 boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE); if (hasInstAwareBpps || needsDepCheck) { //從實(shí)例對(duì)象中提取屬性描述符 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) { //為要設(shè)置的屬性進(jìn)行依賴檢查 checkDependencies(beanName, mbd, filteredPds, pvs); } } //對(duì)屬性進(jìn)行注入 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) { //設(shè)置安全上下文,JDK安全機(jī)制 ((BeanWrapperImpl) bw).setSecurityContext(getAccessControlContext()); } } if (pvs instanceof MutablePropertyValues) { mpvs = (MutablePropertyValues) pvs; //屬性值已經(jīng)轉(zhuǎn)換 if (mpvs.isConverted()) { try { //為實(shí)例化對(duì)象設(shè)置屬性值 bw.setPropertyValues(mpvs); return; } catch (BeansException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Error setting property values", ex); } } //獲取屬性值對(duì)象的原始類型值 original = mpvs.getPropertyValueList(); } else { original = Arrays.asList(pvs.getPropertyValues()); } //獲取用戶自定義的類型轉(zhuǎn)換 TypeConverter converter = getCustomTypeConverter(); if (converter == null) { converter = bw; } //創(chuàng)建一個(gè)Bean定義屬性值解析器,將Bean定義中的屬性值解析為Bean實(shí)例對(duì)象 //的實(shí)際值 BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter); //為屬性的解析值創(chuàng)建一個(gè)拷貝,將拷貝的數(shù)據(jù)注入到實(shí)例對(duì)象中 List<PropertyValue> deepCopy = new ArrayList<PropertyValue>(original.size()); boolean resolveNecessary = false; for (PropertyValue pv : original) { //屬性值不需要轉(zhuǎn)換 if (pv.isConverted()) { deepCopy.add(pv); } //屬性值需要轉(zhuǎn)換 else { String propertyName = pv.getName(); //原始的屬性值,即轉(zhuǎn)換之前的屬性值 Object originalValue = pv.getValue(); //轉(zhuǎn)換屬性值,例如將引用轉(zhuǎn)換為IoC容器中實(shí)例化對(duì)象引用 Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue); //轉(zhuǎn)換之后的屬性值 Object convertedValue = resolvedValue; //屬性值是否可以轉(zhuǎn)換 boolean convertible = bw.isWritableProperty(propertyName) && !PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName); if (convertible) { //使用用戶自定義的類型轉(zhuǎn)換器轉(zhuǎn)換屬性值 convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter); } //存儲(chǔ)轉(zhuǎn)換后的屬性值,避免每次屬性注入時(shí)的轉(zhuǎn)換工作 if (resolvedValue == originalValue) { if (convertible) { //設(shè)置屬性轉(zhuǎn)換之后的值 pv.setConvertedValue(convertedValue); } deepCopy.add(pv); } //屬性是可轉(zhuǎn)換的,且屬性原始值是字符串類型,且屬性的原始類型值不是 //動(dòng)態(tài)生成的字符串,且屬性的原始值不是集合或者數(shù)組類型 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) { //標(biāo)記屬性值已經(jīng)轉(zhuǎn)換過 mpvs.setConverted(); } //進(jìn)行屬性依賴注入 try { bw.setPropertyValues(new MutablePropertyValues(deepCopy)); } catch (BeansException ex) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, "Error setting property values", ex); } }分析上述代碼,我們可以看出,對(duì)屬性的注入過程分以下兩種情況:
(1).屬性值類型不需要轉(zhuǎn)換時(shí),不需要解析屬性值,直接準(zhǔn)備進(jìn)行依賴注入。
(2).屬性值需要進(jìn)行類型轉(zhuǎn)換時(shí),如對(duì)其他對(duì)象的引用等,首先需要解析屬性值,然后對(duì)解析后的屬性值進(jìn)行依賴注入。
對(duì)屬性值的解析是在BeanDefinitionValueResolver類中的resolveValueIfNecessary方法中進(jìn)行的,對(duì)屬性值的依賴注入是通過bw.setPropertyValues方法實(shí)現(xiàn)的,在分析屬性值的依賴注入之前,我們先分析一下對(duì)屬性值的解析過程。
當(dāng)容器在對(duì)屬性進(jìn)行依賴注入時(shí),如果發(fā)現(xiàn)屬性值需要進(jìn)行類型轉(zhuǎn)換,如屬性值是容器中另一個(gè)Bean實(shí)例對(duì)象的引用,則容器首先需要根據(jù)屬性值解析出所引用的對(duì)象,然后才能將該引用對(duì)象注入到目標(biāo)實(shí)例對(duì)象的屬性上去,對(duì)屬性進(jìn)行解析的由resolveValueIfNecessary方法實(shí)現(xiàn),其源碼如下:
//解析屬性值,對(duì)注入類型進(jìn)行轉(zhuǎn)換 public Object resolveValueIfNecessary(Object argName, Object value) { //對(duì)引用類型的屬性進(jìn)行解析 if (value instanceof RuntimeBeanReference) { RuntimeBeanReference ref = (RuntimeBeanReference) value; //調(diào)用引用類型屬性的解析方法 return resolveReference(argName, ref); } //對(duì)屬性值是引用容器中另一個(gè)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; } //對(duì)Bean類型屬性的解析,主要是Bean中的內(nèi)部類 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); } //對(duì)集合數(shù)組類型的屬性解析 else if (value instanceof ManagedArray) { ManagedArray array = (ManagedArray) value; //獲取數(shù)組的類型 Class elementType = array.resolvedElementType; if (elementType == null) { //獲取數(shù)組元素的類型 String elementTypeName = array.getElementTypeName(); if (StringUtils.hasText(elementTypeName)) { try { //使用反射機(jī)制創(chuàng)建指定類型的對(duì)象 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); } } //沒有獲取到數(shù)組的類型,也沒有獲取到數(shù)組元素的類型,則直接設(shè)置數(shù) //組的類型為Object else { elementType = Object.class; } } //創(chuàng)建指定類型的數(shù)組 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其實(shí)就是key和value均為字符串的map else if (value instanceof ManagedProperties) { Properties original = (Properties) value; //創(chuàng)建一個(gè)拷貝,用于作為解析后的返回值 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 { //獲取屬性的目標(biāo)類型 Class<?> resolvedTargetType = resolveTargetType(typedStringValue); if (resolvedTargetType != null) { //對(duì)目標(biāo)類型的屬性進(jìn)行解析,遞歸調(diào)用 return this.typeConverter.convertIfNecessary(valueObject, resolvedTargetType); } //沒有獲取到屬性的目標(biāo)對(duì)象,則按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)); //如果引用的對(duì)象在父類容器中,則從父類容器中獲取指定的引用對(duì)象 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); } //從當(dāng)前的容器中獲取指定的引用Bean對(duì)象,如果指定的Bean沒有被實(shí)例化 //則會(huì)遞歸觸發(fā)引用Bean的初始化和依賴注入 else { Object bean = this.beanFactory.getBean(refName); //將當(dāng)前實(shí)例化對(duì)象的依賴引用對(duì)象 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) { //創(chuàng)建一個(gè)指定類型的數(shù)組,用于存放和返回解析后的數(shù)組 Object resolved = Array.newInstance(elementType, ml.size()); for (int i = 0; i < ml.size(); i++) { //遞歸解析array的每一個(gè)元素,并將解析后的值設(shè)置到resolved數(shù)組中,索引為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的每一個(gè)元素 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的每一個(gè)元素 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中每一個(gè)元素的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是如何將引用類型,內(nèi)部類以及集合類型等屬性進(jìn)行解析的,屬性值解析完成后就可以進(jìn)行依賴注入了,依賴注入的過程就是Bean對(duì)象實(shí)例設(shè)置到它所依賴的Bean對(duì)象屬性上去,在第7步中我們已經(jīng)說過,依賴注入是通過bw.setPropertyValues方法實(shí)現(xiàn)的,該方法也使用了委托模式,在BeanWrapper接口中至少定義了方法聲明,依賴注入的具體實(shí)現(xiàn)交由其實(shí)現(xiàn)類BeanWrapperImpl來完成,下面我們就分析依BeanWrapperImpl中賴注入相關(guān)的源碼。
BeanWrapperImpl類主要是對(duì)容器中完成初始化的Bean實(shí)例對(duì)象進(jìn)行屬性的依賴注入,即把Bean對(duì)象設(shè)置到它所依賴的另一個(gè)Bean的屬性中去,依賴注入的相關(guān)源碼如下:
//實(shí)現(xiàn)屬性依賴注入功能 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 { //獲取屬性值,該方法內(nèi)部使用JDK的內(nèi)省( Introspector)機(jī)制,調(diào)用屬性//的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); } //獲取集合類型屬性的長(zhǎng)度 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); //獲取數(shù)組的類型 Class requiredType = propValue.getClass().getComponentType(); //獲取數(shù)組的長(zhǎng)度 int arrayIndex = Integer.parseInt(key); Object oldValue = null; try { //獲取數(shù)組以前初始化的值 if (isExtractOldValueForEditor()) { oldValue = Array.get(propValue, arrayIndex); } //將屬性的值賦值給數(shù)組中的元素 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的長(zhǎng)度大于屬性值的長(zhǎng)度,則多余的元素賦值為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() + "]"); } } //對(duì)非集合類型的屬性注入 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; } //如果屬性值是必須的,則拋出無法給屬性賦值,因?yàn)槊刻焯峁﹕etter方法異常 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內(nèi)省機(jī)制 final Method readMethod = pd.getReadMethod(); //如果屬性的getter方法不是public訪問控制權(quán)限的,即訪問控制權(quán)限比較嚴(yán)格, //則使用JDK的反射機(jī)制強(qiáng)行訪問非public的方法(暴力讀取屬性值) if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers()) && !readMethod.isAccessible()) { if (System.getSecurityManager()!= null) { //匿名內(nèi)部類,根據(jù)權(quán)限修改屬性的讀取控制限制 AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { readMethod.setAccessible(true); return null; } }); } else { readMethod.setAccessible(true); } } try { //屬性沒有提供getter方法時(shí),調(diào)用潛在的讀取屬性值//的方法,獲取屬性值 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); } } } //設(shè)置屬性的注入值 valueToApply = convertForProperty(propertyName, oldValue, originalValue, pd); } pv.getOriginalPropertyValue().conversionNecessary = (valueToApply != originalValue); } //根據(jù)JDK的內(nèi)省機(jī)制,獲取屬性的setter(寫方法)方法 final Method writeMethod = (pd instanceof GenericTypeAwarePropertyDescriptor ? ((GenericTypeAwarePropertyDescriptor) pd).getWriteMethodForActualAccess() : pd.getWriteMethod()); //如果屬性的setter方法是非public,即訪問控制權(quán)限比較嚴(yán)格,則使用JDK的反射機(jī)制, //強(qiáng)行設(shè)置setter方法可訪問(暴力為屬性賦值) if (!Modifier.isPublic(writeMethod.getDeclaringClass().getModifiers()) && !writeMethod.isAccessible()) { //如果使用了JDK的安全機(jī)制,則需要權(quán)限驗(yàn)證 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 { //將屬性值設(shè)置到屬性上去 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); } } }通過對(duì)上面注入依賴代碼的分析,我們已經(jīng)明白了Spring IoC容器是如何將屬性的值注入到Bean實(shí)例對(duì)象中去的:
(1) 對(duì)于集合類型的屬性,將其屬性值解析為目標(biāo)類型的集合后直接賦值給屬性。
(2) 對(duì)于非集合類型的屬性,大量使用了JDK的反射和內(nèi)省機(jī)制,通過屬性的getter方法(reader method)獲取指定屬性注入以前的值,同時(shí)調(diào)用屬性的setter方法(writer method)為屬性設(shè)置注入后的值。看到這里相信很多人都明白了Spring的setter注入原理。
至此Spring IoC容器對(duì)Bean定義資源文件的定位,載入、解析和依賴注入已經(jīng)全部分析完畢,現(xiàn)在Spring IoC容器中管理了一系列靠依賴關(guān)系聯(lián)系起來的Bean,程序不需要應(yīng)用自己手動(dòng)創(chuàng)建所需的對(duì)象,Spring IoC容器會(huì)在我們使用的時(shí)候自動(dòng)為我們創(chuàng)建,并且為我們注入好相關(guān)的依賴,這就是Spring核心功能的控制反轉(zhuǎn)和依賴注入的相關(guān)功能。
容器其他相關(guān)特性的設(shè)計(jì)與實(shí)現(xiàn)
Application中Bean的初始化及銷毀
簡(jiǎn)要的介紹一下IoC容器中的Bean生命周期:
- Bean實(shí)例的創(chuàng)建
- 為Bean實(shí)例設(shè)置屬性
- 調(diào)用Bean的初始化方法
- 應(yīng)用可以通過IoC容器使用Bean
- 當(dāng)容器關(guān)閉時(shí),調(diào)用Bean的銷毀方法
具體來說:
實(shí)例化一個(gè)Bean--也就是我們常說的new;
按照Spring上下文對(duì)實(shí)例化的Bean進(jìn)行配置--也就是IOC注入;
如果這個(gè)Bean已經(jīng)實(shí)現(xiàn)了BeanNameAware接口,會(huì)調(diào)用它實(shí)現(xiàn)的setBeanName(String)方法,此處傳遞的就是Spring配置文件中Bean的id值;
如果這個(gè)Bean已經(jīng)實(shí)現(xiàn)了BeanFactoryAware接口,會(huì)調(diào)用它實(shí)現(xiàn)的setBeanFactory(setBeanFactory(BeanFactory)傳遞的是Spring工廠自身(可以用這個(gè)方式來獲取其它Bean,只需在Spring配置文件中配置一個(gè)普通的Bean就可以);
如果這個(gè)Bean已經(jīng)實(shí)現(xiàn)了ApplicationContextAware接口,會(huì)調(diào)用setApplicationContext(ApplicationContext)方法,傳入Spring上下文(同樣這個(gè)方式也可以實(shí)現(xiàn)步驟4的內(nèi)容,但比4更好,因?yàn)锳pplicationContext是BeanFactory的子接口,有更多的實(shí)現(xiàn)方法);
如果這個(gè)Bean關(guān)聯(lián)了BeanPostProcessor接口,將會(huì)調(diào)用postProcessBeforeInitialization(Object obj, String s)方法,BeanPostProcessor經(jīng)常被用作是Bean內(nèi)容的更改,并且由于這個(gè)是在Bean初始化結(jié)束時(shí)調(diào)用那個(gè)的方法,也可以被應(yīng)用于內(nèi)存或緩存技術(shù);
如果bean實(shí)現(xiàn)了InitializingBean接口,spring將調(diào)用它的afterPropertiesSet接口方法;如果Bean在Spring配置文件中配置了init-method屬性會(huì)自動(dòng)調(diào)用其配置的初始化方法。
如果這個(gè)Bean關(guān)聯(lián)了BeanPostProcessor接口,將會(huì)調(diào)用postProcessAfterInitialization(Object obj, String s)方法、;
注:以上工作完成以后就可以應(yīng)用這個(gè)Bean了,那這個(gè)Bean是一個(gè)Singleton的,所以一般情況下我們調(diào)用同一個(gè)id的Bean會(huì)是在內(nèi)容地址相同的實(shí)例,當(dāng)然在Spring配置文件中也可以配置非Singleton,這里我們不做贅述。
當(dāng)Bean不再需要時(shí),會(huì)經(jīng)過清理階段,如果Bean實(shí)現(xiàn)了DisposableBean這個(gè)接口,會(huì)調(diào)用那個(gè)其實(shí)現(xiàn)的destroy()方法;
最后,如果這個(gè)Bean的Spring配置中配置了destroy-method屬性,會(huì)自動(dòng)調(diào)用其配置的銷毀方法。
注:以上10步驟可以作為面試或者筆試的模板,另外我們這里描述的是應(yīng)用Spring上下文Bean的生命周期,如果應(yīng)用Spring的工廠也就是BeanFactory的話去掉第5步就Ok了。
lazy-init屬性和預(yù)實(shí)例化
IoC容器的初始化過程就是對(duì)Bean定義資源的定位、載入和注冊(cè),此時(shí)容器對(duì)Bean的依賴注入并沒有發(fā)生,依賴注入主要是在應(yīng)用程序第一次向容器索取Bean時(shí),通過getBean方法的調(diào)用完成。 當(dāng)Bean定義資源的< Bean >元素中配置了lazy-init屬性時(shí),容器將會(huì)在初始化的時(shí)候?qū)λ渲玫腂ean進(jìn)行預(yù)實(shí)例化,Bean的依賴注入在容器初始化的時(shí)候就已經(jīng)完成。這樣,當(dāng)應(yīng)用程序第一次向容器索取被管理的Bean時(shí),就不用再初始化和對(duì)Bean進(jìn)行依賴注入了,直接從容器中獲取已經(jīng)完成依賴注入的現(xiàn)成Bean,可以提高應(yīng)用第一次向容器獲取Bean的性能。 當(dāng)Bean定義資源被載入IoC容器之后,容器將Bean定義資源解析為容器內(nèi)部的數(shù)據(jù)結(jié)構(gòu)BeanDefinition注冊(cè)到容器中,AbstractApplicationContext類中的finishBeanFactoryInitialization方法對(duì)配置了預(yù)實(shí)例化屬性的Bean進(jìn)行預(yù)初始化過程。
FactoryBean的實(shí)現(xiàn)
FactoryBean是一個(gè)典型的工廠模式的使用。它是一個(gè)Bean,作用是產(chǎn)生其他bean實(shí)例。通常情況下,僅需要提供一個(gè)工廠方法,該方法用來返回其他Bean實(shí)例。通常情況下,bean無須自己實(shí)現(xiàn)工廠模式,Spring容器擔(dān)任工廠角色;但少數(shù)情況下,容器中的bean本身就是工廠,其作用是產(chǎn)生其它Bean實(shí)例。
當(dāng)用戶使用容器本身時(shí),可以使用轉(zhuǎn)義字符”&”來得到FactoryBean本身,以區(qū)別通過FactoryBean產(chǎn)生的實(shí)例對(duì)象和FactoryBean對(duì)象本身。在BeanFactory中通過如下代碼定義了該轉(zhuǎn)義字符。
BeanPostProcessor的實(shí)現(xiàn)
BeanPostProcessor后置處理器是Spring IoC容器經(jīng)常使用到的一個(gè)特性,這個(gè)Bean后置處理器是一個(gè)監(jiān)聽器,可以監(jiān)聽容器觸發(fā)的Bean聲明周期事件。后置處理器向容器注冊(cè)以后,容器中管理的Bean就具備了接收IoC容器事件回調(diào)的能力。 BeanPostProcessor的使用非常簡(jiǎn)單,只需要提供一個(gè)實(shí)現(xiàn)接口BeanPostProcessor的實(shí)現(xiàn)類,然后在Bean的配置文件中設(shè)置即可。
package org.springframework.beans.factory.config; import org.springframework.beans.BeansException; public interface BeanPostProcessor { //為在Bean的初始化前提供回調(diào)入口 Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; //為在Bean的初始化之后提供回調(diào)入口 Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; }這兩個(gè)回調(diào)的入口都是和容器管理的Bean的生命周期事件緊密相關(guān),可以為用戶提供在Spring IoC容器初始化Bean過程中自定義的處理操作。
autowiring(自動(dòng)裝配的實(shí)現(xiàn))
在自動(dòng)裝配中,Spring IoC容器的依賴自動(dòng)裝配功能,不需要對(duì)Bean屬性的依賴關(guān)系做顯式的聲明,只需要在配置好autowiring屬性,IoC容器會(huì)自動(dòng)使用反射查找屬性的類型和名稱,然后基于屬性的類型或者名稱來自動(dòng)匹配容器中管理的Bean,從而自動(dòng)地完成依賴注入。
應(yīng)用第一次通過getBean方法(配置了lazy-init預(yù)實(shí)例化屬性的除外)向IoC容器索取Bean時(shí),容器創(chuàng)建Bean實(shí)例對(duì)象,并且對(duì)Bean實(shí)例對(duì)象進(jìn)行屬性依賴注入,AbstractAutoWireCapableBeanFactory的populateBean方法就是實(shí)現(xiàn)Bean屬性依賴注入的功能。
在populateBean的實(shí)現(xiàn)中,在處理一般的Bean之前,先對(duì)autowiring屬性進(jìn)行處理。如果當(dāng)前的Bean配置了autowire_by_name和autowire_by_type屬性,那么調(diào)用相應(yīng)的autowireByName方法autowireByType方法。對(duì)于autowire_by_name,它首先通過反射機(jī)制從當(dāng)前Bean中得到需要注入的屬性名,然后使用這個(gè)屬性名向容器申請(qǐng)與之同名的Bean,這樣實(shí)際又觸發(fā)了另一個(gè)Bean的生成和依賴注入的過程。
autowiring的實(shí)現(xiàn)過程:
a. 對(duì)Bean的屬性迭代調(diào)用getBean方法,完成依賴Bean的初始化和依賴注入。
b. 將依賴Bean的屬性引用設(shè)置到被依賴的Bean屬性上。
c. 將依賴Bean的名稱和被依賴的Bean的名稱存儲(chǔ)在IoC容器的集合中。
Bean的依賴檢查
通過依賴檢查特性,Spring可以幫助應(yīng)用檢查是否所有的屬性已經(jīng)被正確設(shè)置。在具體使用的時(shí)候,應(yīng)用只需要在Bean定義中設(shè)置dependency-check屬性來指定依賴檢查模式即可。
這里可以將屬性設(shè)置為none、simple、object、all四種模式,默認(rèn)的模式是none。
總結(jié)
以上是生活随笔為你收集整理的Spring IoC容器设计原理及高级特性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度 orc识别图片转文字
- 下一篇: gradle idea java ssm