Spring 同一接口注入多個bean實現


如果同一個接口有多個bean實現類,spring在運行時會出現如下異常

required a single bean, but 2 were found:
marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

@Primary的意思是在眾多相同的bean中,優先使用用@Primary注解的bean.而@Qualifier這個注解則指定某個bean有沒有資格進行注入。

@Component
@Primary

 

@Autowired

 

@Component

@Qualifier("fjaoidfj")

 

@Autowired

@Qualifier("fjaoidfj")

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM