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