springboot 循环依赖问题


springboot 循环依赖问题

背景

项目联合开发,也不知道谁制造的BUG

异常详情

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

image-20220317155607395

解决方案一:

使用Bean标签注入的方式 添加lazy-init属性

<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>

解决方案二:

使用注解方式:

@Lazy

导包:

org.springframework.context.annotation.Lazy

image-20220317164354385

总结

两种方案的目的都是让其中一个Bean先加载完成【IOC初始化加载类 首先条件为:单例,非懒加载】,在加载另外一个懒加载类;【我们将另外一个类设置为懒加载,即可避免同时加载两个类,产生循环依赖问题】


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM