加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – Spring Boot:当自动装配具体类时,“找不到类型的限定

发布时间:2020-12-15 01:39:05 所属栏目:大数据 来源:网络整理
导读:我正在使用Spring Boot和Spring Boot JPA编写一个组件.我有这样的设置: 界面: public interface Something { // method definitions} 实施: @Componentpublic class SomethingImpl implements Something { // implementation} 现在,我有一个使用SpringJUn

我正在使用Spring Boot和Spring Boot JPA编写一个组件.我有这样的设置:

界面:

public interface Something {
    // method definitions
}

实施:

@Component
public class SomethingImpl implements Something {
    // implementation
}

现在,我有一个使用SpringJUnit4ClassRunner运行的JUnit测试,我想用它来测试我的SomethingImpl.

当我做

@Autowired
private Something _something;

它有效,但是

@Autowired
private SomethingImpl _something;

导致测试失败抛出NoSuchBeanDefinitionException并且消息没有找到类型为[com.example.SomethingImpl]的限定bean依赖:预期至少有1个bean有资格作为此依赖项的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

但是在测试用例中,我想明确地注入我的SomethingImpl,因为它是我想要测试的类.我怎么做到这一点?

最佳答案
如果你想要一个特殊的bean,你必须使用@Qualifier注释:

@Autowired
@Qualifier("SomethingImpl")
private Something _something;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读