Could not autowire. There is more than one bean of ‘ xxx ‘ type.

1、问题描述
有时候我们使用@Autowired,注入自己写的一个bean对象时,IDEA有报错提示There is more than one bean of 'CustomerRentFeign' type. 虽然可以忽略,但是 也可以 彻底解决。

  • spring容器中 本身就有一个CustomerRentFeign类型的代理对象, 你又重新声明了一个该类型的对象,所以报错 Could not autowire. There is more than one bean of ’ xxx ’ type.
  • 如果一个类型 有两个对象放到IOC容器中的话,会报错 There is more than one bean of
    ‘CustomerRentFeign’ type.
    在这里插入图片描述

2、解决方法

2.1 使用@Resource (推荐)

  • @Resource 是根据 对象名字 区分的, 所以不会重复
  • @Autowired 是根据 类型 区分的,所以同一个类型的 两个对象 会重复

在这里插入图片描述
2.2 @Autowired 搭配 @Qualifier
@Qualifier注解里面填写的值就是当前类注入到IOC容器的bean的唯一标识(id)
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45432276/article/details/132121666