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