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

Spring循環(huán)依賴的解決方案詳解

 更新時間:2022年07月09日 09:46:07   作者:IT利刃出鞘  
這篇文章將用實例為大家詳細介紹了介紹如何解決Spring的循環(huán)依賴問題,文中的示例代碼講解詳細,對我們學習Spring有一定的幫助,需要的可以參考一下

簡介

說明

本文用實例介紹如何解決Spring的循環(huán)依賴問題。

相關網(wǎng)址

Spring循環(huán)依賴之問題復現(xiàn)詳解

公共代碼

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();
    }
}

方案1. Feild注入單例(@AutoWired)

代碼

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

測試

啟動不報錯。

postman訪問:http://localhost:8080/test1

后端結果:不報錯

postman結果: 20Tony

方案2. 構造器注入+@Lazy

延遲加載:在注入依賴時,先注入代理對象,當首次使用時再創(chuàng)建對象完成注入。

代碼

package com.example.tmp;
 
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
 
@Component
public class A {
    private B b;
 
    public A(@Lazy B b) {
        this.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.context.annotation.Lazy;
import org.springframework.stereotype.Component;
 
@Component
public class B {
    private A a;
 
    public B(@Lazy A a) {
        this.a = a;
    }
 
    private Integer age = 20;
 
    public Integer getAge() {
        return age;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
 
}

測試

啟動不報錯。

postman訪問:http://localhost:8080/test1

后端結果:不報錯

postman結果: 20Tony

方案3. Setter/Field注入單例

代碼

package com.example.tmp;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class A {
    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;
    }
 
    public B getB() {
        return b;
    }
 
    @Autowired
    public void setB(B b) {
        this.b = b;
    }
}
package com.example.tmp;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class B {
    private A a;
 
    private Integer age = 20;
 
    public Integer getAge() {
        return age;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
 
    public A getA() {
        return a;
    }
 
    @Autowired
    public void setA(A a) {
        this.a = a;
    }
}

測試 

啟動不報錯。

postman訪問:http://localhost:8080/test1

后端結果:不報錯

postman結果: 20Tony

方案4. @PostConstruct

代碼

package com.example.tmp;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
 
@Component
public class A {
    @Autowired
    private B b;
 
    @PostConstruct
    public void init() {
        b.setA(this);
    }
 
    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;
    }
 
    public A getA() {
        return a;
    }
 
    public void setA(A a) {
        this.a = a;
    }
}

測試 

啟動不報錯。

postman訪問:http://localhost:8080/test1

后端結果:不報錯

postman結果: 20Tony

方案5. 實現(xiàn)ApplicationContextAware與InitializingBean

代碼 

package com.example.tmp;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
@Component
public class A implements ApplicationContextAware, InitializingBean {
    private B b;
 
    private ApplicationContext context;
 
    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;
    }
 
    @Override
    public void afterPropertiesSet() throws Exception {
        this.b = context.getBean(B.class);
    }
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}
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;
    }
}

測試 

啟動不報錯。

postman訪問:http://localhost:8080/test1

后端結果:不報錯

postman結果: 20Tony

以上就是Spring循環(huán)依賴的解決方案詳解的詳細內容,更多關于Spring循環(huán)依賴的資料請關注腳本之家其它相關文章!

相關文章

  • JAVA內存溢出解決方案圖解

    JAVA內存溢出解決方案圖解

    這篇文章主要介紹了JAVA內存溢出解決方案圖解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10
  • Spring Boot 配置 Quartz 定時任務的方法

    Spring Boot 配置 Quartz 定時任務的方法

    這篇文章主要介紹了Spring Boot 配置 Quartz 定時任務的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Java利用自定義注解、反射實現(xiàn)簡單BaseDao實例

    Java利用自定義注解、反射實現(xiàn)簡單BaseDao實例

    下面小編就為大家?guī)硪黄狫ava利用自定義注解、反射實現(xiàn)簡單BaseDao實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java?Controller實現(xiàn)參數(shù)驗證與統(tǒng)一異常處理流程詳細講解

    Java?Controller實現(xiàn)參數(shù)驗證與統(tǒng)一異常處理流程詳細講解

    Controller是Spring接受并處理網(wǎng)頁請求的組件,是整個應用的入口,因此學會Controller的常用注解對理解一個應用是重中之重。SpringBoot的Controller中經(jīng)常會用到注解@Controller、@RestController、@RequestMapping、@RequestBody等
    2023-01-01
  • Java實現(xiàn)遞歸刪除菜單和目錄及目錄下所有文件

    Java實現(xiàn)遞歸刪除菜單和目錄及目錄下所有文件

    這篇文章主要為大家詳細介紹了Java如何實現(xiàn)遞歸刪除菜單和刪除目錄及目錄下所有文件,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下
    2025-03-03
  • JAVA開發(fā)常用類庫UUID、Optional、ThreadLocal、TimerTask、Base64使用方法與實例詳解

    JAVA開發(fā)常用類庫UUID、Optional、ThreadLocal、TimerTask、Base64使用方法與實例詳

    這篇文章主要介紹了JAVA開發(fā)常用類庫UUID、Optional、ThreadLocal、TimerTask、Base64使用方法與實例詳解,需要的朋友可以參考下
    2020-02-02
  • Java實現(xiàn)解壓zip壓縮包的兩種方法(支持多層級)

    Java實現(xiàn)解壓zip壓縮包的兩種方法(支持多層級)

    壓縮文件在生活中經(jīng)常能用到,在Java中提供了壓縮和解壓縮文件的功能,本文主要介紹了Java實現(xiàn)解壓zip壓縮包的兩種方法(支持多層級),感興趣的可以了解一下
    2024-03-03
  • SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解

    SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解

    這篇文章主要介紹了SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解,ApplicationListener是應用程序的事件監(jiān)聽器,繼承自java.util.EventListener標準接口,采用觀察者設計模式,需要的朋友可以參考下
    2023-11-11
  • hibernate中的對象關系映射

    hibernate中的對象關系映射

    hibernate中的ORM映射文件通常以.hbm.xml作為后綴。使用這個映射文件不僅易讀,而且可以手工修改,也可以通過一些工具來生成映射文檔,下文給大家詳細的介紹hibernate中的對象關系映射,需要的朋友參考下吧
    2017-09-09
  • SpringBoot集成ActiveMQ的實戰(zhàn)全過程

    SpringBoot集成ActiveMQ的實戰(zhàn)全過程

    消息隊列中間件是分布式系統(tǒng)中重要的組件,主要解決應用耦合、異步消息、流量削鋒等問題,實現(xiàn)高性能、高可用、可伸縮和最終一致性架構,是大型分布式系統(tǒng)不可缺少的中間件,這篇文章主要給大家介紹了關于SpringBoot集成ActiveMQ的相關資料,需要的朋友可以參考下
    2021-11-11

最新評論

洱源县| 井陉县| 湘阴县| 连平县| 安新县| 涡阳县| 平邑县| 珠海市| 绵阳市| 比如县| 崇信县| 呼和浩特市| 襄垣县| 绥化市| 苏尼特左旗| 茌平县| 伊宁县| 江西省| 兴城市| 北京市| 静乐县| 布尔津县| 新晃| 秦安县| 五峰| 高陵县| 临沧市| 红安县| 龙南县| 阳曲县| 静宁县| 淮阳县| 阿拉善盟| 塔城市| 洞口县| 彰武县| 石嘴山市| 綦江县| 通辽市| 西安市| 木兰县|