一文搞懂Spring循環(huán)依賴的原理
簡(jiǎn)介
說明
本文用實(shí)例來介紹@Autowired解決循環(huán)依賴的原理。@Autowired是通過三級(jí)緩存來解決循環(huán)依賴的。
除了@Autoired,還有其他方案來解決循環(huán)依賴的,見:Spring循環(huán)依賴的解決方案詳解
概述
@Autowired進(jìn)行屬性注入可以解決循環(huán)依賴。原理是:Spring控制了bean的生命周期,先實(shí)例化bean,后注入bean的屬性。Spring中記錄了正在創(chuàng)建中的bean(已經(jīng)實(shí)例化但還沒初始化完畢的bean),所以可以在注入屬性時(shí),從記錄的bean中取依賴的對(duì)象。
相對(duì)而言,單純使用構(gòu)造器注入就無法解決循環(huán)依賴。因?yàn)椋跇?gòu)造時(shí)就需要傳入依賴的對(duì)象,導(dǎo)致無法實(shí)例化。(注意:構(gòu)造器注入可以使用@Lazy解決循環(huán)依賴,在實(shí)例化時(shí),傳入代理對(duì)象,真正使用時(shí)才會(huì)生成真正的對(duì)象)
循環(huán)依賴實(shí)例
代碼
package com.example.tmp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class A {
@Autowired
private B b;
private String name = "Tony";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTest() {
return b.getAge().toString() + name;
}
}
package com.example.tmp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class B {
@Autowired
private A a;
private Integer age = 20;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.example.controller;
import com.example.tmp.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private A a;
@GetMapping("/test1")
public String test1() {
return a.getTest();
}
}
測(cè)試
1.啟動(dòng)不報(bào)錯(cuò)。
2.postman訪問:http://localhost:8080/test1
后端結(jié)果:不報(bào)錯(cuò)
postman結(jié)果: 20Tony
到此這篇關(guān)于一文搞懂Spring循環(huán)依賴的原理的文章就介紹到這了,更多相關(guān)Spring循環(huán)依賴內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java動(dòng)態(tài)代理機(jī)制詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java動(dòng)態(tài)代理機(jī)制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Mybatis實(shí)體類對(duì)象入?yún)⒉樵兊墓P記
這篇文章主要介紹了Mybatis實(shí)體類對(duì)象入?yún)⒉樵兊墓P記,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
SpringMVC中事務(wù)是否可以加在Controller層的問題
這篇文章主要介紹了SpringMVC中事務(wù)是否可以加在Controller層的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
springboot如何從數(shù)據(jù)庫獲取數(shù)據(jù),用echarts顯示(數(shù)據(jù)可視化)
這篇文章主要介紹了springboot如何從數(shù)據(jù)庫獲取數(shù)據(jù),用echarts顯示(數(shù)據(jù)可視化),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
使用Spring Boot創(chuàng)建Web應(yīng)用程序的示例代碼
本篇文章主要介紹了使用Spring Boot創(chuàng)建Web應(yīng)用程序的示例代碼,我們將使用Spring Boot構(gòu)建一個(gè)簡(jiǎn)單的Web應(yīng)用程序,并為其添加一些有用的服務(wù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
fastjson對(duì)JSONObject中的指定字段重新賦值的實(shí)現(xiàn)
這篇文章主要介紹了fastjson對(duì)JSONObject中的指定字段重新賦值的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
java如何將實(shí)體類轉(zhuǎn)換成json并在控制臺(tái)輸出
這篇文章主要介紹了java如何將實(shí)體類轉(zhuǎn)換成json并在控制臺(tái)輸出問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11

