Spring循環(huán)依賴的解決方案詳解
簡介
說明
本文用實例介紹如何解決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
延遲加載:在注入依賴時,先注入代理對象,當(dāng)首次使用時再創(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)依賴的解決方案詳解的詳細(xì)內(nèi)容,更多關(guān)于Spring循環(huán)依賴的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
String類型傳遞是值傳遞,char[]類型傳遞是引用傳遞的實現(xiàn)
下面小編就為大家?guī)硪黄猄tring類型傳遞是值傳遞,char[]類型傳遞是引用傳遞的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看不2016-09-09
IntelliJ IDEA右鍵文件夾沒有Java Class文件的原因及解決方法
這篇文章主要介紹了IntelliJ IDEA右鍵文件夾沒有Java Class文件的原因及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09
Springboot?整合?RocketMQ?收發(fā)消息的配置過程
這篇文章主要介紹了Springboot?整合?RocketMQ?收發(fā)消息,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
SpringBoot整合RabbitMQ的5種模式實戰(zhàn)
SpringCloud讀取Nacos配置中心報錯及遇到的坑:Could?not?resolve?placehold

