欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

簡介

說明

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

相關(guān)網(wǎng)址

Spring循環(huán)依賴之問題復(fù)現(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

后端結(jié)果:不報錯

postman結(jié)果: 20Tony

方案2. 構(gòu)造器注入+@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

后端結(jié)果:不報錯

postman結(jié)果: 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

后端結(jié)果:不報錯

postman結(jié)果: 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

后端結(jié)果:不報錯

postman結(jié)果: 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

后端結(jié)果:不報錯

postman結(jié)果: 20Tony

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

相關(guān)文章

  • SpringBoot整合RabbitMQ的5種模式實戰(zhàn)

    SpringBoot整合RabbitMQ的5種模式實戰(zhàn)

    本文主要介紹了SpringBoot整合RabbitMQ的5種模式實戰(zhàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java中URL傳中文時亂碼的解決方法

    Java中URL傳中文時亂碼的解決方法

    為什么說亂碼是中國程序員無法避免的話題呢?這個主要是編碼機制上的原因,大家都知道中文和英文的編碼格式不一樣,解碼自然也不一樣!這篇文章就給大家分享了Java中URL傳中文時亂碼的解決方法,有需要的朋友們可以參考借鑒。
    2016-10-10
  • SpringCloud讀取Nacos配置中心報錯及遇到的坑:Could?not?resolve?placeholder?‘xxx’?in?value?‘${xxx}

    SpringCloud讀取Nacos配置中心報錯及遇到的坑:Could?not?resolve?placehold

    這篇文章主要介紹了SpringCloud讀取Nacos配置中心報錯:Could?not?resolve?placeholder?‘xxx’?in?value?‘${xxx},本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • java中String的常見用法總結(jié)

    java中String的常見用法總結(jié)

    以下是關(guān)于string的七種用法,注意哦,記得要時常去查看java的API文檔,那個里面也有很詳細的介紹
    2013-10-10
  • 最新評論