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
解決方案一:
使用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
總結
兩種方案的目的都是讓其中一個Bean先加載完成【IOC初始化加載類 首先條件為:單例,非懶加載】,在加載另外一個懶加載類;【我們將另外一個類設置為懶加載,即可避免同時加載兩個類,產生循環依賴問題】