spring 循环依赖解决,spring 循环依赖怎么解决?
目录
简介方案1.费尔德注入单例(@自动连线)方案2.构造器注入@懒方案3.设定者/字段注入单例方案4.@PostConstruct方案5.实现ApplicationContextAware与回调接口
简介
说明
本文用实例介绍如何解决春天的循环依赖问题。
相关网址
春天循环依赖之问题复现详解
公共代码
包com。举例。控制器;导入com。举例。tmp。a;导入org。spring框架。豆子。工厂。注释。自动连线;导入org。spring框架。网络。绑定。注释。获取映射;导入org。spring框架。网络。绑定。注释。休息控制器;@ RestControllerpublic class hello controller { @ auto wired private A A;@GetMapping(/test1 )公共字符串test1(){ return a . gettest();}}
方案1. Feild注入单例(@AutoWired)
代码
包com。举例。tmp导入org。spring框架。豆子。工厂。注释。自动连线;导入org。spring框架。刻板印象。组件;@组件公共类A { @自动有线私有B B私有字符串托尼公共字符串getName(){ return name;} public void set name(String name){ this。name=名称;}公共字符串getTest() { return b.getAge().toString()名称;} }包com。举例。tmp导入org。spring框架。豆子。工厂。注释。自动连线;导入org。spring框架。刻板印象。组件;@组件公共类B { @自动有线私有A A私人整数年龄=20;public Integer getAge(){ return age;} public void setAge(整数年龄){这个。年龄=年龄;} }测试
启动不报错。
邮递员访问:http://localhost:8080/test1
后端结果:不报错
邮递员结果:伦敦20区
方案2. 构造器注入+@Lazy
延迟加载:在注入依赖时,先注入代理对象,当首次使用时再创建对象完成注入。
代码
包com。举例。tmp导入org。spring框架。语境。注释。懒惰;导入org。spring框架。刻板印象。组件;@组件公共类A {私有B B公A(@懒B B){这个。B=B}私有字符串名称=托尼公共字符串getName(){ return name;} public void set name(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
方案3. Setter/Field注入单例
代码
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
方案4. @PostConstruct
代码
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
方案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; @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循环依赖的解决方案详解的详细内容,更多关于Spring循环依赖的资料请关注盛行IT其它相关文章!
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。