问题:当一个接口实现由两个实现类时,只使用@Autowired注解,会报错,以下图所示spring
实现类1express
实现类2ide
controller中注入工具
而后启动服务报错,以下所示:idea
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderController': Unsatisfied dependency expressed through field 'productOrderService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'net.xdclass.order_service.service.ProductOrderService' available: expected single matching bean but found 2: service1,service2blog
缘由是存在两个实例service1,service2,系统不知道注入哪一个一个实例,其实idea工具已经飘红提示了存在两个实例,因此这里咱们就须要用到@Qualifier注解来指明注入的实例,以下图所示接口
这样就ok了,idea飘红提示也没有了,启动也正常it
补充:咱们也能够用@Resource(name="service1")如图所示io