成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

[[407544]]

是這樣的,有個(gè)實(shí)習(xí)生朋友問了我循環(huán)依賴的問題,我將Spring內(nèi)部的三級緩存原理都跟他說了,并保證Spring已經(jīng)解決了這個(gè)問題,然后他扔了一道題給我,說報(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,原來Spring并沒有解決構(gòu)造器循環(huán)依賴

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

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

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

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

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

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

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

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

責(zé)任編輯:武曉燕 來源: 稀飯下雪
相關(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-11-22 09:30:50

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

2023-12-14 12:56:00

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

2012-03-13 11:21:34

索尼AndroidVita OS

2017-12-07 08:56:21

2020-08-11 10:20:26

http數(shù)據(jù)庫狀態(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

2024-07-29 07:02:00

OpenAIGPT-4oAI

2022-08-18 08:41:32

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

2021-05-10 11:04:46

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

2023-05-04 08:06:27

Spring循環(huán)依賴

2023-01-12 16:57:39

ChatGPT

2021-02-02 18:03:00

字符串面試官子序列

2023-07-11 16:01:47

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

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

主站蜘蛛池模板: 久久成人综合 | 日韩在线观看精品 | 亚洲一区二区三区欧美 | 在线视频91| 国产一区在线免费 | 成人av高清在线观看 | 久久久久久久久久毛片 | 久久99这里只有精品 | 久草久 | 欧美日韩一区二区三区视频 | 国产精品伦理一区 | 欧美一级黄色片在线观看 | 亚洲国产欧美在线人成 | 伊人精品在线视频 | 亚洲精品成人网 | 欧美自拍第一页 | 精品视频一区二区在线观看 | 国产欧美视频一区 | 亚洲国产精品久久 | 久久精品综合 | 日批的视频| 又爽又黄axxx片免费观看 | 狠狠躁天天躁夜夜躁婷婷老牛影视 | 亚欧洲精品在线视频免费观看 | 日韩亚洲一区二区 | 日本高清aⅴ毛片免费 | 国产激情精品视频 | 久久久亚洲成人 | 日韩精品一区二区三区第95 | 在线中文字幕av | 国产蜜臀 | 欧美成人精品 | 精品国产乱码久久久久久蜜退臀 | 午夜在线免费观看 | 激情欧美日韩一区二区 | 自拍偷拍亚洲欧美 | 国产高清视频在线 | 久久久av | 久久亚洲欧美日韩精品专区 | 成人国产精品久久 | 国产成人精品一区二区三区在线 |