日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Spring解決循環(huán)依賴的3種方式!

循環(huán)依賴就是N個(gè)類中循環(huán)嵌套引用,如果在日常開發(fā)中我們用new 對(duì)象的方式發(fā)生這種循環(huán)依賴的話程序會(huì)在運(yùn)行時(shí)一直循環(huán)調(diào)用,直至內(nèi)存溢出報(bào)錯(cuò)。

創(chuàng)新互聯(lián)公司成立與2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、成都做網(wǎng)站網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元綿陽做網(wǎng)站,已為上家服務(wù),為綿陽各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792

下面說一下Spring是如果解決循環(huán)依賴的。

第一種:構(gòu)造器參數(shù)循環(huán)依賴

Spring容器會(huì)將每一個(gè)正在創(chuàng)建的Bean 標(biāo)識(shí)符放在一個(gè)“當(dāng)前創(chuàng)建Bean池”中,Bean標(biāo)識(shí)符在創(chuàng)建過程中將一直保持在這個(gè)池中。

因此如果在創(chuàng)建Bean過程中發(fā)現(xiàn)自己已經(jīng)在“當(dāng)前創(chuàng)建Bean池”里時(shí)將拋出BeanCurrentlyInCreationException異常表示循環(huán)依賴;而對(duì)于創(chuàng)建完畢的Bean將從“當(dāng)前創(chuàng)建Bean池”中清除掉。

首先我們先初始化三個(gè)Bean。

 
 
 
 
  1. public class StudentA {  
  2.     private StudentB studentB ;  
  3.     public void setStudentB(StudentB studentB) {  
  4.         this.studentB = studentB;  
  5.     }  
  6.     public StudentA() {  
  7.     }  
  8.     public StudentA(StudentB studentB) {  
  9.         this.studentB = studentB;  
  10.     }  
  11. }  
 
 
 
 
  1. public class StudentB {  
  2.     private StudentC studentC ;  
  3.     public void setStudentC(StudentC studentC) {  
  4.         this.studentC = studentC;  
  5.     }  
  6.     public StudentB() {  
  7.     }  
  8.     public StudentB(StudentC studentC) {  
  9.         this.studentC = studentC;  
  10.     }  
  11. }  
 
 
 
 
  1. public class StudentC {  
  2.     private StudentA studentA ;  
  3.     public void setStudentA(StudentA studentA) {  
  4.         this.studentA = studentA;  
  5.     }  
  6.     public StudentC() {  
  7.     }  
  8.     public StudentC(StudentA studentA) {  
  9.         this.studentA = studentA;  
  10.     }  

OK,上面是很基本的3個(gè)類,StudentA有參構(gòu)造是StudentB。StudentB的有參構(gòu)造是StudentC,StudentC的有參構(gòu)造是StudentA ,這樣就產(chǎn)生了一個(gè)循環(huán)依賴的情況。

我們都把這三個(gè)Bean交給 Spring 管理,并用有參構(gòu)造實(shí)例化。

 
 
 
 
  1.   
  2.     
  3.   
  4.   
  5.     
  6.   
  7.   
  8.     
  9.  

下面是測(cè)試類:

 
 
 
 
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         ApplicationContext context = new ClassPathXmlApplicationContext("com/zfx/student/applicationContext.xml");  
  4.         //System.out.println(context.getBean("a", StudentA.class));  
  5.     }  

執(zhí)行結(jié)果報(bào)錯(cuò)信息為:

 
 
 
 
  1. Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException:  
  2.   Error creating bean with name 'a': Requested bean is currently in creation: Is there an unresolvable circular reference? 

如果大家理解開頭那句話的話,這個(gè)報(bào)錯(cuò)應(yīng)該不驚訝,Spring容器先創(chuàng)建單例StudentA,StudentA依賴StudentB,然后將A放在“當(dāng)前創(chuàng)建Bean池”中。

此時(shí)創(chuàng)建StudentB,StudentB依賴StudentC ,然后將B放在“當(dāng)前創(chuàng)建Bean池”中,此時(shí)創(chuàng)建StudentC,StudentC又依賴StudentA。

但是,此時(shí)Student已經(jīng)在池中,所以會(huì)報(bào)錯(cuò),因?yàn)樵诔刂械腂ean都是未初始化完的,所以會(huì)依賴錯(cuò)誤 ,初始化完的Bean會(huì)從池中移除。

第二種:setter方式單例,默認(rèn)方式

如果要說setter方式注入的話,我們最好先看一張Spring中Bean實(shí)例化的圖

如圖中前兩步驟得知:Spring是先將Bean對(duì)象實(shí)例化之后再設(shè)置對(duì)象屬性的,Spring 中的 bean 為什么默認(rèn)單例, 這篇建議大家看下。

關(guān)注微信公眾號(hào):Java技術(shù)棧,在后臺(tái)回復(fù):spring,可以獲取我整理的 N 篇最新 Spring 教程,都是干貨。

修改配置文件為set方式注入

 
 
 
 
  1.   
  2.   
  3.     
  4.   
  5.   
  6.     
  7.   
  8.   
  9.     
  10.  

下面是測(cè)試類:

 
 
 
 
  1. public class Test {  
  2.     public static void main(String[] args) { 
  3.         ApplicationContext context = new ClassPathXmlApplicationContext("com/zfx/student/applicationContext.xml");  
  4.         System.out.println(context.getBean("a", StudentA.class));  
  5.     }  

打印結(jié)果為:

 
 
 
 
  1. com.zfx.student.StudentA@1fbfd6 

為什么用set方式就不報(bào)錯(cuò)了呢 ?

我們結(jié)合上面那張圖看,Spring先是用構(gòu)造實(shí)例化Bean對(duì)象 ,此時(shí) Spring 會(huì)將這個(gè)實(shí)例化結(jié)束的對(duì)象放到一個(gè)Map中,并且 Spring 提供了獲取這個(gè)未設(shè)置屬性的實(shí)例化對(duì)象引用的方法。

結(jié)合我們的實(shí)例來看,當(dāng)Spring實(shí)例化了StudentA、StudentB、StudentC后,緊接著會(huì)去設(shè)置對(duì)象的屬性,此時(shí)StudentA依賴StudentB,就會(huì)去Map中取出存在里面的單例StudentB對(duì)象,以此類推,不會(huì)出來循環(huán)的問題嘍、

下面是Spring源碼中的實(shí)現(xiàn)方法。以下的源碼在Spring的Bean包中的DefaultSingletonBeanRegistry.java類中

 
 
 
 
  1. /** Cache of singleton objects: bean name --> bean instance(緩存單例實(shí)例化對(duì)象的Map集合) */  
  2. private final Map singletonObjects = new ConcurrentHashMap(64);  
  3. /** Cache of singleton factories: bean name --> ObjectFactory(單例的工廠Bean緩存集合) */  
  4. private final Map singletonFactories = new HashMap(16);  
  5. /** Cache of early singleton objects: bean name --> bean instance(早期的單身對(duì)象緩存集合) */  
  6. private final Map earlySingletonObjects = new HashMap(16);  
  7. /** Set of registered singletons, containing the bean names in registration order(單例的實(shí)例化對(duì)象名稱集合) */  
  8. private final Set registeredSingletons = new LinkedHashSet(64);  
  9. /**  
  10.  * 添加單例實(shí)例  
  11.  * 解決循環(huán)引用的問題  
  12.  * Add the given singleton factory for building the specified singleton  
  13.  * if necessary.  
  14.  * 

    To be called for eager registration of singletons, e.g. to be able to  

  15.  * resolve circular references.  
  16.  * @param beanName the name of the bean  
  17.  * @param singletonFactory the factory for the singleton object  
  18.  */  
  19. protected void addSingletonFactory(String beanName, ObjectFactory singletonFactory) {  
  20.   Assert.notNull(singletonFactory, "Singleton factory must not be null");  
  21.   synchronized (this.singletonObjects) {  
  22.     if (!this.singletonObjects.containsKey(beanName)) {  
  23.       this.singletonFactories.put(beanName, singletonFactory);  
  24.       this.earlySingletonObjects.remove(beanName);  
  25.       this.registeredSingletons.add(beanName);  
  26.     }  
  27.   }  

第三種:setter方式原型,prototype

修改配置文件為:

 
 
 
 
  1.   
  2.     
  3.   
  4.   
  5.     
  6.   
  7.   
  8.     
  9.  

scope="prototype" 意思是 每次請(qǐng)求都會(huì)創(chuàng)建一個(gè)實(shí)例對(duì)象。

兩者的區(qū)別是:有狀態(tài)的bean都使用Prototype作用域,無狀態(tài)的一般都使用singleton單例作用域。

測(cè)試用例:

 
 
 
 
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         ApplicationContext context = new ClassPathXmlApplicationContext("com/zfx/student/applicationContext.xml");  
  4.         //此時(shí)必須要獲取Spring管理的實(shí)例,因?yàn)楝F(xiàn)在scope="prototype" 只有請(qǐng)求獲取的時(shí)候才會(huì)實(shí)例化對(duì)象  
  5.         System.out.println(context.getBean("a", StudentA.class));  
  6.     }  

打印結(jié)果:

 
 
 
 
  1. Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException:  
  2.     Error creating bean with name 'a': Requested bean is currently in creation: Is there an unresolvable circular reference? 

為什么原型模式就報(bào)錯(cuò)了呢 ?

對(duì)于“prototype”作用域Bean,Spring容器無法完成依賴注入,因?yàn)椤皃rototype”作用域的Bean,Spring容器不進(jìn)行緩存,因此無法提前暴露一個(gè)創(chuàng)建中的Bean。


網(wǎng)頁標(biāo)題:Spring解決循環(huán)依賴的3種方式!
文章URL:http://m.5511xx.com/article/dpcjscj.html