最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

淺談Spring解決循環(huán)依賴的三種方式

 更新時間:2017年11月18日 11:28:41   作者:學習園  
本篇文章主要介紹了淺談Spring循環(huán)依賴的三種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

引言:循環(huán)依賴就是N個類中循環(huán)嵌套引用,如果在日常開發(fā)中我們用new 對象的方式發(fā)生這種循環(huán)依賴的話程序會在運行時一直循環(huán)調用,直至內存溢出報錯。下面說一下Spring是如果解決循環(huán)依賴的。

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

表示通過構造器注入構成的循環(huán)依賴,此依賴是無法解決的,只能拋出BeanCurrentlyIn CreationException異常表示循環(huán)依賴。

如在創(chuàng)建TestA類時,構造器需要TestB類,那將去創(chuàng)建TestB,在創(chuàng)建TestB類時又發(fā)現(xiàn)需要TestC類,則又去創(chuàng)建TestC,最終在創(chuàng)建TestC時發(fā)現(xiàn)又需要TestA,從而形成一個環(huán),沒辦法創(chuàng)建。

Spring容器會將每一個正在創(chuàng)建的Bean 標識符放在一個“當前創(chuàng)建Bean池”中,Bean標識符在創(chuàng)建過程中將一直保持
在這個池中,因此如果在創(chuàng)建Bean過程中發(fā)現(xiàn)自己已經(jīng)在“當前創(chuàng)建Bean池”里時將拋出
BeanCurrentlyInCreationException異常表示循環(huán)依賴;而對于創(chuàng)建完畢的Bean將從“當前創(chuàng)建Bean池”中清除掉。

首先我們先初始化三個Bean。

public class StudentA { 
 
 private StudentB studentB ; 
 
 public void setStudentB(StudentB studentB) { 
 this.studentB = studentB; 
 } 
 
 public StudentA() { 
 } 
 
 public StudentA(StudentB studentB) { 
 this.studentB = studentB; 
 } 
} 

public class StudentB { 
 
 private StudentC studentC ; 
 
 public void setStudentC(StudentC studentC) { 
 this.studentC = studentC; 
 } 
 
 public StudentB() { 
 } 
 
 public StudentB(StudentC studentC) { 
 this.studentC = studentC; 
 } 
} 
public class StudentC { 
 
 private StudentA studentA ; 
 
 public void setStudentA(StudentA studentA) { 
 this.studentA = studentA; 
 } 
 
 public StudentC() { 
 } 
 
 public StudentC(StudentA studentA) { 
 this.studentA = studentA; 
 } 
} 

OK,上面是很基本的3個類,,StudentA有參構造是StudentB。StudentB的有參構造是StudentC,StudentC的有參構造是StudentA ,這樣就產(chǎn)生了一個循環(huán)依賴的情況,我們都把這三個Bean交給Spring管理,并用有參構造實例化

<bean id="a" class="com.zfx.student.StudentA"> 
 <constructor-arg index="0" ref="b"></constructor-arg> 
</bean> 
<bean id="b" class="com.zfx.student.StudentB"> 
 <constructor-arg index="0" ref="c"></constructor-arg> 
</bean> 
<bean id="c" class="com.zfx.student.StudentC"> 
 <constructor-arg index="0" ref="a"></constructor-arg> 
</bean> 

下面是測試類:

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

執(zhí)行結果報錯信息為:

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

如果大家理解開頭那句話的話,這個報錯應該不驚訝,Spring容器先創(chuàng)建單例StudentA,StudentA依賴StudentB,然后將A放在“當前創(chuàng)建Bean池”中,此時創(chuàng)建StudentB,StudentB依賴StudentC ,然后將B放在“當前創(chuàng)建Bean池”中,此時創(chuàng)建StudentC,StudentC又依賴StudentA, 但是,此時Student已經(jīng)在池中,所以會報錯,,因為在池中的Bean都是未初始化完的,所以會依賴錯誤 ,(初始化完的Bean會從池中移除)

第二種:setter方式單例,默認方式

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

如圖中前兩步驟得知:Spring是先將Bean對象實例化之后再設置對象屬性的

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

<!--scope="singleton"(默認就是單例方式) --> 
<bean id="a" class="com.zfx.student.StudentA" scope="singleton"> 
 <property name="studentB" ref="b"></property> 
</bean> 
<bean id="b" class="com.zfx.student.StudentB" scope="singleton"> 
 <property name="studentC" ref="c"></property> 
</bean> 
<bean id="c" class="com.zfx.student.StudentC" scope="singleton"> 
 <property name="studentA" ref="a"></property> 
</bean> 

下面是測試類:

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

打印結果為:

com.zfx.student.StudentA@1fbfd6 

為什么用set方式就不報錯了呢 ?

我們結合上面那張圖看,Spring先是用構造實例化Bean對象 ,此時Spring會將這個實例化結束的對象放到一個Map中,并且Spring提供了獲取這個未設置屬性的實例化對象引用的方法。   結合我們的實例來看,,當Spring實例化了StudentA、StudentB、StudentC后,緊接著會去設置對象的屬性,此時StudentA依賴StudentB,就會去Map中取出存在里面的單例StudentB對象,以此類推,不會出來循環(huán)的問題嘍、

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

/** Cache of singleton objects: bean name --> bean instance(緩存單例實例化對象的Map集合) */ 
 private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(64); 
 
 /** Cache of singleton factories: bean name --> ObjectFactory(單例的工廠Bean緩存集合) */ 
 private final Map<String, ObjectFactory> singletonFactories = new HashMap<String, ObjectFactory>(16); 
 
 /** Cache of early singleton objects: bean name --> bean instance(早期的單身對象緩存集合) */ 
 private final Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16); 
 
 /** Set of registered singletons, containing the bean names in registration order(單例的實例化對象名稱集合) */ 
 private final Set<String> registeredSingletons = new LinkedHashSet<String>(64); 
 /** 
 * 添加單例實例 
 * 解決循環(huán)引用的問題 
 * Add the given singleton factory for building the specified singleton 
 * if necessary. 
 * <p>To be called for eager registration of singletons, e.g. to be able to 
 * resolve circular references. 
 * @param beanName the name of the bean 
 * @param singletonFactory the factory for the singleton object 
 */ 
 protected void addSingletonFactory(String beanName, ObjectFactory singletonFactory) { 
 Assert.notNull(singletonFactory, "Singleton factory must not be null"); 
 synchronized (this.singletonObjects) { 
  if (!this.singletonObjects.containsKey(beanName)) { 
  this.singletonFactories.put(beanName, singletonFactory); 
  this.earlySingletonObjects.remove(beanName); 
  this.registeredSingletons.add(beanName); 
  } 
 } 
 } 

第三種:setter方式原型,prototype

對于"prototype"作用域bean,Spring容器無法完成依賴注入,因為Spring容器不進行緩存"prototype"作用域的bean,因此無法提前暴露一個創(chuàng)建中的bean。

修改配置文件為:

<bean id="a" class="com.zfx.student.StudentA" <span style="color:#FF0000;">scope="prototype"</span>> 
 <property name="studentB" ref="b"></property> 
 </bean> 
 <bean id="b" class="com.zfx.student.StudentB" <span style="color:#FF0000;">scope="prototype"</span>> 
 <property name="studentC" ref="c"></property> 
 </bean> 
 <bean id="c" class="com.zfx.student.StudentC" <span style="color:#FF0000;">scope="prototype"</span>> 
 <property name="studentA" ref="a"></property> 
 </bean> 

scope="prototype" 意思是 每次請求都會創(chuàng)建一個實例對象。兩者的區(qū)別是:有狀態(tài)的bean都使用Prototype作用域,無狀態(tài)的一般都使用singleton單例作用域。

測試用例:

public class Test { 
 public static void main(String[] args) { 
 ApplicationContext context = new ClassPathXmlApplicationContext("com/zfx/student/applicationContext.xml"); 
 <strong>//此時必須要獲取Spring管理的實例,因為現(xiàn)在scope="prototype" 只有請求獲取的時候才會實例化對象</strong> 
 System.out.println(context.getBean("a", StudentA.class)); 
 } 
} 

打印結果:

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

為什么原型模式就報錯了呢 ?

對于“prototype”作用域Bean,Spring容器無法完成依賴注入,因為“prototype”作用域的Bean,Spring容
器不進行緩存,因此無法提前暴露一個創(chuàng)建中的Bean。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java服務調用失敗報Service com.oneinfinite.adflow.api.service.TestService未找到的解決方法

    Java服務調用失敗報Service com.oneinfinite.adflow.api.service.T

    在Java開發(fā)中,服務調用是常見的操作,尤其是在微服務架構中,然而,服務調用過程中可能會遇到各種問題,下面我們來看看如何解決Service com.oneinfinite.adflow.api.service.TestService with version 0.0.0 not found的問題吧
    2025-03-03
  • Maven項目打包為jar的四種方式

    Maven項目打包為jar的四種方式

    本文主要介紹了Maven項目打包為jar的四種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-10-10
  • java 文件名截取方法

    java 文件名截取方法

    在實際開發(fā)應用中會應到截取文件名,本文將介紹java中文件名的截取,需要了解的朋友可以參考下
    2012-11-11
  • 詳解MyBatis的XML實現(xiàn)方法(附帶注解方式實現(xiàn))

    詳解MyBatis的XML實現(xiàn)方法(附帶注解方式實現(xiàn))

    這篇文章主要詳細介紹了MyBatis的XML實現(xiàn)方法(附帶注解方式實現(xiàn)),文中通過代碼示例給大家講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-05-05
  • IDEA如何讓控制臺自動換行

    IDEA如何讓控制臺自動換行

    本文介紹了如何在IDEA中設置控制臺自動換行,具體步驟為:File -> Settings -> Editor -> General -> Console,然后勾選"Use soft wraps in console"選項
    2025-01-01
  • Spring實現(xiàn)默認標簽解析流程

    Spring實現(xiàn)默認標簽解析流程

    這篇文章主要為大家詳細介紹了Spring實現(xiàn)默認標簽解析流程,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Jenkins的安裝配置詳解

    Jenkins的安裝配置詳解

    這篇文章主要介紹了Jenkins的安裝配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • spring XML配置文件標簽詳解

    spring XML配置文件標簽詳解

    這篇文章主要介紹了spring XML配置文件標簽詳解,本文給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2024-12-12
  • java實現(xiàn)猜拳游戲

    java實現(xiàn)猜拳游戲

    這篇文章主要為大家詳細介紹了java實現(xiàn)猜拳游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Java中多線程Reactor模式的實現(xiàn)

    Java中多線程Reactor模式的實現(xiàn)

    多線程Reactor模式旨在分配多個reactor每一個reactor獨立擁有一個selector,本文就詳細的來介紹一下Java中多線程Reactor模式的實現(xiàn),需要的朋友可以參考下
    2021-12-12

最新評論

安塞县| 四川省| 博客| 新郑市| 广州市| 瓮安县| 滦平县| 曲周县| 松原市| 卓尼县| 思南县| 虞城县| 辽阳市| 普洱| 崇州市| 冷水江市| 枣庄市| 利津县| 西藏| 监利县| 苏州市| 道真| 察雅县| 错那县| 铜山县| 封开县| 虹口区| 德昌县| 额济纳旗| 思南县| 滨海县| 增城市| 云龙县| 康平县| 乐昌市| 印江| 辽宁省| 绥棱县| 河源市| 达州市| 宕昌县|