No qualifying bean of type [java.lang.String] is defined: expected single matching bean but found 2

Spring注解@Autowired、@Resource、@Qualifier使用及区别 解决:

  • expected single matching bean but found 2
  • No qualifying bean of type [java.lang.String] is defined: expected single matching bean but found 2:
    这些错误都是由于Spring容器自动装配无法匹配合适的bean引起的,值得说明的是,在使用配置文件方式配置bean时,在配置文件中配置bean时,需要特别注意那个 name 属性,而不是 id 属性,文末举例。

在平时的spring项目中,经常看到@Autowired、@Resource、@Qualifier等注解,这篇文章就给大家分享一下我的使用经验。

1. @Autowired

使用@Autowired注解自动注入需要的引用,下面给出简单示例。

  1. 定义一个人类接口
package com.exc.demo.annotest;

public interface People {
   
    String getSex();
}
  1. 分别创建“男人”和“女人”2个类来实现上边的人类接口
package com.exc.demo.annotest;

public class Man implements People {
   
    @Override
    public String getSex() {
   
        return "男人";
    }
}
package com.exc.demo.annotest;

public class Woman implements People {
   
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值