Springboot循环依赖解决办法
2017-11-07 15:14
2923 查看
最近在使用Spingboot做项目的时候,在引入shiro
后,启动项目一直报错 Error creating bean with name 'debtServiceImpl': Bean with name 'debtServiceImpl' has been injected into other beans [repayBillServiceImpl,investServiceImpl,receiveBillServiceImpl] in its raw version as part of a circular reference, but has eventually
been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example. 后来在网上找了半天说是依赖循环,检查了一下代码,确实存在循环依赖的现象,但是项目快要上线,再去改代码逻辑是来不及了,于是各种找解决方案,终于算是找到了。
首先说一下什么是依赖循环,
比如:我现在有一个ServiceA需要调用ServiceB的方法,那么ServiceA就依赖于ServiceB,那在ServiceB中再调用ServiceA的方法,就形成了循环依赖。Spring在初始化bean的时候就不知道先初始化哪个
bean就会报错。
那如何解决循环依赖,当然最好的方法是重构你的代码,进行解耦,但是重构不是一时的事情,那就使用下面的方法:
第一种:
<bean id="ServiceDependent1" class="org.xyz.ServiceDependent1" lazy-init="true">
<constructor-arg ref="Service"/> </bean>
<bean id="ServiceDependent2" class="org.xyz.ServiceDependent2" lazy-init="true">
<constructor-arg ref="Service"/> </bean>
在你的配置文件中,在互相依赖的两个bean的任意一个加上lazy-init属性。
第二种:
@Autowired
@Lazy
private ClassA classA;
@Autowired
@Lazy
private ClassB classB;
在你注入bean时,在互相依赖的两个bean上加上@Lazy注解也可以。
以上两种方法都能延迟互相依赖的其中一个bean的加载,从而解决循环依赖的问题。
后,启动项目一直报错 Error creating bean with name 'debtServiceImpl': Bean with name 'debtServiceImpl' has been injected into other beans [repayBillServiceImpl,investServiceImpl,receiveBillServiceImpl] in its raw version as part of a circular reference, but has eventually
been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching - consider using 'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example. 后来在网上找了半天说是依赖循环,检查了一下代码,确实存在循环依赖的现象,但是项目快要上线,再去改代码逻辑是来不及了,于是各种找解决方案,终于算是找到了。
首先说一下什么是依赖循环,
比如:我现在有一个ServiceA需要调用ServiceB的方法,那么ServiceA就依赖于ServiceB,那在ServiceB中再调用ServiceA的方法,就形成了循环依赖。Spring在初始化bean的时候就不知道先初始化哪个
bean就会报错。
public class ClassA { @Autowired ClassB classB; }
public class ClassB { @Autowired ClassA classA }
那如何解决循环依赖,当然最好的方法是重构你的代码,进行解耦,但是重构不是一时的事情,那就使用下面的方法:
第一种:
<bean id="ServiceDependent1" class="org.xyz.ServiceDependent1" lazy-init="true">
<constructor-arg ref="Service"/> </bean>
<bean id="ServiceDependent2" class="org.xyz.ServiceDependent2" lazy-init="true">
<constructor-arg ref="Service"/> </bean>
在你的配置文件中,在互相依赖的两个bean的任意一个加上lazy-init属性。
第二种:
@Autowired
@Lazy
private ClassA classA;
@Autowired
@Lazy
private ClassB classB;
在你注入bean时,在互相依赖的两个bean上加上@Lazy注解也可以。
以上两种方法都能延迟互相依赖的其中一个bean的加载,从而解决循环依赖的问题。
相关文章推荐
- Springboot循环依赖解决办法
- spring boot/cloud 多服务部署单机启动顺序有依赖的解决办法
- SpringBoot中contraller即使启动类放在同包下也会报404的解决办法
- spring boot controller设置 @Transactional 不回滚的解决办法
- spring-boot项目启动失败的一种解决办法:自带的tomcat容器切换成jetty容器
- Intellij IDEA SpringBoot热部署 使用Spring-boot-devTools无效解决办法
- Spring-boot-devTools无效解决办法
- SpringBoot页面表单请求失败,错误400的解决办法
- shiro+spring boot+mybatis启动循环引用问题解决思路和方案
- spring-boot上传文件最佳解决办法
- spring boot web项目在IDEA下热部署解决办法(四步搞定)
- 初学Spring Boot遇到Whitelabel Error Page的解决办法
- spring boot项目 maven项目导入eclipse自动maven update 后jre不是1.8而是1.6解决办法
- SpringBoot自定义配置文件fatjar获取不到解决办法
- Intellij IDEA 使用Spring-boot-devTools无效解决办法
- CocoaPods 0.35.0版本, 出现循环依赖的解决办法
- SPRING循环依赖(circular reference)的解决方法
- 外部Tomcat部署Spring boot自动关闭的解决办法
- spring boot创建项目包依赖问题的解决
- Spring中的循环依赖问题介绍及解决方法