zoukankan      html  css  js  c++  java
  • @Autowired注解与@Qualifier注解搭配使用

    问题:当一个接口实现由两个实现类时,只使用@Autowired注解,会报错,如下图所示

    实现类1

     实现类2

     controller中注入

    然后启动服务报错,如下所示:

    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,service2

    原因是存在两个实例service1,service2,系统不知道注入哪个一个实例,其实idea工具已经飘红提示了存在两个实例,所以这里我们就需要用到@Qualifier注解来指明注入的实例,如下图所示

    这样就ok了,idea飘红提示也没有了,启动也正常

    补充:我们也可以用@Resource(name="service1")如图所示

  • 相关阅读:
    我爱工程化 之 gulp 使用(二)
    我爱工程化 之 gulp 使用(一)
    用户体验之输入框设想
    SEO优化
    js代码优化
    RequireJs 依赖管理使用
    Git 安装与使用(一)
    Webstorm 配置与使用 Less
    Less使用——让老司机带你飞
    Node安装与环境配置
  • 原文地址:https://www.cnblogs.com/hjw-zq/p/10626347.html
Copyright © 2011-2022 走看看