問題:當一個接口實現由兩個實現類時,只使用@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