偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

高級(jí)開(kāi)發(fā)竟然被構(gòu)造器循環(huán)依賴難住了?

開(kāi)發(fā) 前端
是這樣的,有個(gè)實(shí)習(xí)生朋友問(wèn)了我循環(huán)依賴的問(wèn)題,我將Spring內(nèi)部的三級(jí)緩存原理都跟他說(shuō)了,并保證Spring已經(jīng)解決了這個(gè)問(wèn)題,然后他扔了一道題給我,說(shuō)報(bào)錯(cuò)了。

[[407544]]

是這樣的,有個(gè)實(shí)習(xí)生朋友問(wèn)了我循環(huán)依賴的問(wèn)題,我將Spring內(nèi)部的三級(jí)緩存原理都跟他說(shuō)了,并保證Spring已經(jīng)解決了這個(gè)問(wèn)題,然后他扔了一道題給我,說(shuō)報(bào)錯(cuò)了。

好家伙,感情是想我讓我查bug

你們看看?

  1. @Component 
  2. public class A { 
  3.  
  4.     private final B b; 
  5.  
  6.     public A(final B b) { 
  7.         this.b = b; 
  8.     } 
  9.  
  10.     public void print() { 
  11.         System.out.println("in a"); 
  12.     } 
  1. @Component 
  2. public class B { 
  3.  
  4.     private final A a; 
  5.  
  6.     public B(final A a) { 
  7.         this.a = a; 
  8.     } 
  9.  
  10.     public void print() { 
  11.         System.out.println("in b"); 
  12.     } 
  1. Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'a' defined in file [C:\soft\code\common\MongodbDataTest\dbDataTest\target\test-classes\com\db\model\A.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'b' defined in file [C:\soft\code\common\MongodbDataTest\dbDataTest\target\test-classes\com\db\model\B.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'a': Requested bean is currently in creation: Is there an unresolvable circular reference? 
  2.  at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) 
  3.  at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) 
  4.  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354) 
  5.  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204) 
  6.  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) 
  7.  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) 
  8.  at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) 
  9.  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) 
  10.  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) 
  11.  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) 
  12.  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) 
  13.  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) 
  14.  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) 
  15.  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782) 
  16.  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:774) 
  17.  at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) 
  18.  at org.springframework.boot.SpringApplication.run(SpringApplication.java:339) 
  19.  at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:123) 
  20.  at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) 
  21.  at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) 
  22.  ... 68 more 
  23. Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'b' defined in file [C:\soft\code\common\MongodbDataTest\dbDataTest\target\test-classes\com\db\model\B.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'a': Requested bean is currently in creation: Is there an unresolvable circular reference? 

這TM,原來(lái)Spring并沒(méi)有解決構(gòu)造器循環(huán)依賴

難道,spring對(duì)于這種循環(huán)依賴真的束手無(wú)策了么?

其實(shí)不是的,spring還有@Lazy這個(gè)大殺器...只需要我們對(duì)剛剛那兩個(gè)類(lèi)小小的改造一下:

lazy為啥可以解決這個(gè)問(wèn)題?

反調(diào)@Lazy注解可以看到

從源碼我們可以看到,對(duì)于@Lazy的依賴,我們其實(shí)是返回了一個(gè)代理類(lèi)(以下稱為L(zhǎng)azyProxy)而不是正真通過(guò)getBean拿到目標(biāo)bean注入。

而真正的獲取bean的邏輯,被封裝到了一個(gè)TargetSource類(lèi)的getTarget方法中,而這個(gè)TargetSource類(lèi)最終被用來(lái)生成LazyProxy了,那么我們是不是可以推測(cè),LazyProxy應(yīng)該持有這個(gè)TargetSource對(duì)象。

而從我們懶加載的語(yǔ)意來(lái)講,是說(shuō)真正使用到這個(gè)bean(調(diào)用這個(gè)bean的某個(gè)方法時(shí))的時(shí)候,才對(duì)這個(gè)屬性進(jìn)行注入/初始化。

那么對(duì)于當(dāng)前這個(gè)例子來(lái)講,就是說(shuō)其實(shí)B創(chuàng)建的時(shí)候,并沒(méi)有去調(diào)用getBean("a")去獲取構(gòu)造器的參數(shù),而是直接生成了一個(gè)LazyProxy來(lái)做B構(gòu)造器的參數(shù),而B(niǎo)之后正真調(diào)用到A的方法時(shí),才會(huì)去調(diào)用TargetSource中的getTarget獲取A實(shí)例,即調(diào)用getBean("a"),這個(gè)時(shí)候A早就實(shí)例化好了,所以也就不會(huì)有循環(huán)依賴問(wèn)題了。

責(zé)任編輯:武曉燕 來(lái)源: 稀飯下雪
相關(guān)推薦

2023-02-17 07:27:28

2021-06-11 06:38:25

CTO瀏覽器文件

2021-07-07 11:15:05

文件前端瀏覽器

2021-01-07 08:23:02

日志

2024-12-19 15:41:17

2023-12-14 12:56:00

鏈?zhǔn)?/a>調(diào)用代碼

2023-11-22 09:30:50

e簽寶面試企業(yè)面經(jīng)

2012-03-13 11:21:34

索尼AndroidVita OS

2017-12-07 08:56:21

2020-08-11 10:20:26

http數(shù)據(jù)庫(kù)狀態(tài)

2024-11-07 08:28:53

2009-07-21 12:35:00

Scala從構(gòu)造器

2020-04-30 10:24:35

Spring循環(huán)依賴Java

2022-08-18 08:41:32

RPC微服務(wù)事件驅(qū)動(dòng)

2024-07-29 07:02:00

OpenAIGPT-4oAI

2023-05-04 08:06:27

Spring循環(huán)依賴

2021-05-10 11:04:46

Windows 操作系統(tǒng)微軟

2023-01-12 16:57:39

ChatGPT

2021-02-02 18:03:00

字符串面試官子序列

2023-07-11 16:01:47

共享數(shù)據(jù)開(kāi)發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)