自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring解決循環(huán)依賴的3種方式!

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

[[314515]]

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

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

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

Spring容器會將每一個正在創(chuàng)建的Bean 標識符放在一個“當前創(chuàng)建Bean池”中,Bean標識符在創(chuàng)建過程中將一直保持在這個池中。

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

首先我們先初始化三個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個類,StudentA有參構造是StudentB。StudentB的有參構造是StudentC,StudentC的有參構造是StudentA ,這樣就產生了一個循環(huán)依賴的情況。

我們都把這三個Bean交給 Spring 管理,并用有參構造實例化。 

  1. <bean id="a" class="com.zfx.student.StudentA">  
  2.   <constructor-arg index="0" ref="b"></constructor-arg>  
  3. </bean>  
  4. <bean id="b" class="com.zfx.student.StudentB">  
  5.   <constructor-arg index="0" ref="c"></constructor-arg>  
  6. </bean>  
  7. <bean id="c" class="com.zfx.student.StudentC">  
  8.   <constructor-arg index="0" ref="a"></constructor-arg>  
  9. </bean> 

下面是測試類: 

  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í)行結果報錯信息為: 

  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? 

如果大家理解開頭那句話的話,這個報錯應該不驚訝,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已經在池中,所以會報錯,因為在池中的Bean都是未初始化完的,所以會依賴錯誤 ,初始化完的Bean會從池中移除。

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

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

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

關注微信公眾號:Java技術棧,在后臺回復:spring,可以獲取我整理的 N 篇最新 Spring 教程,都是干貨。

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

  1. <!--scope="singleton"(默認就是單例方式) -->  
  2. <bean id="a" class="com.zfx.student.StudentA" scope="singleton">  
  3.   <property name="studentB" ref="b"></property>  
  4. </bean>  
  5. <bean id="b" class="com.zfx.student.StudentB" scope="singleton">  
  6.   <property name="studentC" ref="c"></property>  
  7. </bean>  
  8. <bean id="c" class="com.zfx.student.StudentC" scope="singleton">  
  9.   <property name="studentA" ref="a"></property>  
  10. </bean> 

下面是測試類: 

  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.     }  

打印結果為: 

  1. 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類中 

  1. /** Cache of singleton objects: bean name --> bean instance(緩存單例實例化對象的Map集合) */  
  2. private final Map<String, Object> singletonObjects = new ConcurrentHashMap<String, Object>(64);  
  3. /** Cache of singleton factories: bean name --> ObjectFactory(單例的工廠Bean緩存集合) */  
  4. private final Map<String, ObjectFactory> singletonFactories = new HashMap<String, ObjectFactory>(16);  
  5. /** Cache of early singleton objects: bean name --> bean instance(早期的單身對象緩存集合) */  
  6. private final Map<String, Object> earlySingletonObjects = new HashMap<String, Object>(16);  
  7. /** Set of registered singletons, containing the bean names in registration order(單例的實例化對象名稱集合) */  
  8. private final Set<String> registeredSingletons = new LinkedHashSet<String>(64);  
  9. /**  
  10.  * 添加單例實例  
  11.  * 解決循環(huán)引用的問題  
  12.  * Add the given singleton factory for building the specified singleton  
  13.  * if necessary.  
  14.  * <p>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. <bean id="a" class="com.zfx.student.StudentA" scope="prototype">  
  2.   <property name="studentB" ref="b"></property>  
  3. </bean>  
  4. <bean id="b" class="com.zfx.student.StudentB" scope="prototype">  
  5.   <property name="studentC" ref="c"></property>  
  6. </bean>  
  7. <bean id="c" class="com.zfx.student.StudentC" scope="prototype">  
  8.   <property name="studentA" ref="a"></property>  
  9. </bean> 

scope="prototype" 意思是 每次請求都會創(chuàng)建一個實例對象。

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

測試用例: 

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

打印結果: 

  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? 

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

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

 

責任編輯:龐桂玉 來源: Java技術棧
相關推薦

2019-11-26 14:30:20

Spring循環(huán)依賴Java

2009-09-08 15:22:20

Spring依賴注入

2020-12-29 08:34:08

spring循環(huán)依賴開發(fā)

2023-10-07 08:40:57

緩存屬性Spring

2024-04-12 07:51:05

SpringBean初始化

2022-08-17 07:52:31

Spring循環(huán)依賴單例池

2023-11-28 08:00:00

SpringJava

2022-05-08 19:23:28

Spring循環(huán)依賴

2023-05-04 08:06:27

Spring循環(huán)依賴

2023-12-12 17:44:13

三級緩存Bean

2025-03-17 00:21:00

2020-06-22 08:07:48

Spring依賴場景

2025-03-20 08:00:00

@LazySpring開發(fā)

2021-09-01 11:45:10

Spring循環(huán)依賴面試

2024-06-05 11:43:10

2021-05-06 07:58:57

Spring BeanIOCAOP

2023-05-26 07:19:49

Spring聲明式事務

2024-03-18 00:00:00

SpringBean設計

2012-07-17 09:16:16

SpringSSH

2011-02-28 13:51:30

Spring事物配置
點贊
收藏

51CTO技術棧公眾號