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")

 

posted @ 2018-06-21 12:01  yangfei969  阅读(8328)  评论(0编辑  收藏  举报