本文小编为大家详细介绍“如何解决spring循环依赖问题”,内容详细,步骤清晰,细节处理妥当,希望这篇“如何解决Spring循环依赖问题”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。公共代码package&nbs
本文小编为大家详细介绍“如何解决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; @RestControllerpublic class HelloController { @Autowired private A a; @GetMapping("/test1") public String test1() { return a.getTest(); }}
代码
package com.example.tmp; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component; @Componentpublic 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; @Componentpublic 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
延迟加载:在注入依赖时,先注入代理对象,当首次使用时再创建对象完成注入。
代码
package com.example.tmp; import org.springframework.context.annotation.Lazy;import org.springframework.stereotype.Component; @Componentpublic 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; @Componentpublic 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
代码
package com.example.tmp; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component; @Componentpublic 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; @Componentpublic 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
代码
package com.example.tmp; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Componentpublic 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; @Componentpublic 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
代码
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; @Componentpublic 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; @Componentpublic 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循环依赖问题”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程网精选频道。
--结束END--
本文标题: 如何解决Spring循环依赖问题
本文链接: https://www.lsjlt.com/news/343096.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0