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初始化加载类 首先条件为:单例,非懒加载】,在加载另外一个懒加载类;【我们将另外一个类设置为懒加载,即可避免同时加载两个类,产生循环依赖问题】

posted @ 2022-03-17 16:50  Mr*宇晨  阅读(762)  评论(0编辑  收藏  举报