您现在的位置是:群英 > 开发技术 > 编程语言
Spring循环依赖的解决有多少办法,怎样做?
Admin发表于 2022-10-11 09:18:05337 次浏览
今天这篇给大家分享的知识是“Spring循环依赖的解决有多少办法,怎样做?”,小编觉得挺不错的,对大家学习或是工作可能会有所帮助,因此分享发大家做个参考,下文的讲解详细,步骤过程清晰,希望这篇“Spring循环依赖的解决有多少办法,怎样做?”文章能帮助大家解决问题。

 

简介

说明

本文用实例介绍如何解决Spring的循环依赖问题。

相关网址

Spring循环依赖之问题复现详解

公共代码

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

延迟加载:在注入依赖时,先注入代理对象,当首次使用时再创建对象完成注入。

代码

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. 实现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循环依赖的解决有多少办法,怎样做?”的内容了,通过以上内容的阐述,相信大家对Spring循环依赖的解决有多少办法,怎样做?已经有了进一步的了解,如果想要了解更多相关的内容,欢迎关注群英网络,群英网络将为大家推送更多相关知识点的文章。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

标签: Spring循环依赖
相关信息推荐
2022-05-16 17:19:20 
摘要:bootstrap模态框的用法:1、通过data属性,同时设置“data-target="#myModal"”选择器内容;2、通过js,直接用代码“$(’#myModal’).modal(options)”等等。
2022-07-23 17:41:58 
摘要:本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于批量修改文件名的相关问题,包括了在原有的名字前中后批量加字、所有文件重新命名并添加序号等等内容,下面一起来看一下,希望对大家有帮助。
2022-05-19 17:39:51 
摘要:go语言既不是面向对象,也不是面向过程,因为Golang并没有明显的倾向,而是更倾向于让编程者去考虑该怎么去用它,也许它的特色就是灵活,编程者可以用它实现面向对象,但它本身不支持面向对象的语义。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部