栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

No qualifying bean of type问题解决

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

No qualifying bean of type问题解决

No qualifying bean of type问题解决 前言

No qualifying bean of type ‘xxx.xxx.xxx’ available: expected single matching bean but found 2:xxx.xxx.xxx

对于一个java开发新手来说, 应该经常会遇到这个问题,而且还不好意思询问周围同事,不要怕,今天就让我们解决掉它。

问题及解决方案 问题1 包扫描不正确或未扫描
  • 默认情况下,如果使用了springboot,他会默认扫描启动类(使用了注解@SpringBootApplication)所在的包及子包,扫描的包中是否包含当前类所在的包。
  • 检查 scanBasePackages扫描路径是否包含当前类所在的包。
解决方案
  • 将当前类所在的包加入扫描路径即可,例如当前类所在包为 com.demo1

    @SpringBootApplication(@SpringBootApplication(scanBasePackages = {"com.demo","com.demo1"}))
    

    切记不要忘了扫描当前启动类所在的包 ,否则又会导致其他类不能注入。

问题2 当前类是否注入了spring环境
  • 当前类是否有 @Service @Component等注解
  • 当前类是否被@Bean初始化
解决方案
  • 增加对应注解将类注入到spring环境即可,例如:

    @Service
    public class ServiceImpl1 implements Service{}
    或
    @Component
    public class ServiceImpl1 implements Service{}
    或
    @Bean
    public ServiceImpl serviceImpl(){
    	return new ServiceImpl();
    }
    

    使用@Bean方法所在的类也必须是要注入到spring环境中的。

问题3 多个类实现了同一个接口
  • 查看当前接口是否被多个类实现

  • 多个实现类是否未设置BeanName或者设置了相同的BeanName,例如:

    @Service
    public class ServiceImpl1 implements Service{}
    @Service
    public class ServiceImpl2 implements Service{}
    或
    @Service("Service1")
    public class ServiceImpl3 implements Service{}
    @Service("Service1")
    public class ServiceImpl4 implements Service{}
    

    以上方式都会出现异常。

  • 注入Bean时是否指定了设置的BeanName,例如:

     @Autowired
     private Service service;
    
解决方案

当我们需要多个类实现同一个接口时,需要为每个实现类配置BeanName,例如:

@Service("serviceImpl3")
public class ServiceImpl3 implements Service{}
@Service("serviceImpl2")
public class ServiceImpl2 implements Service{}
@Service("serviceImpl1")
public class ServiceImpl1 implements Service{}

建议首字母小写。

以上没有问题,那么使用的时候还需要指定BeanName,例如:

@Qualifier(value = "serviceImpl1")
@Autowired
private Service service;

关于@Qualifier使用,详见:Spring @Qualifier 注释

如果不想在每个使用处设置@Qualifier,只想在特殊场景下设置,那么只需要在默认的实现类上增加注解@Primary,例如:

@Primary
@Service
public class ServiceImpl implements Service{}

那么在使用的时候就可以直接注入了,默认会使用ServiceImpl,例如:

@Autowired
private Service service;
注意事项

当存在多个相同类型的Bean,强烈建议使用@Primary把其中某一个Bean标识为“主要的”,使用@Autowired注入时会首先使用被标记为@Primary的Bean。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/866012.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号