Spring Boot:在自动装配具体类时没有"找到类型的限定bean"

rab*_*ens 7 java spring hibernate spring-mvc spring-boot

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

界面:

public interface Something {
    // method definitions
}
Run Code Online (Sandbox Code Playgroud)

实施:

@Component
public class SomethingImpl implements Something {
    // implementation
}
Run Code Online (Sandbox Code Playgroud)

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

当我做

@Autowired
private Something _something;
Run Code Online (Sandbox Code Playgroud)

它有效,但是

@Autowired
private SomethingImpl _something;
Run Code Online (Sandbox Code Playgroud)

导致测试失败抛出一条NoSuchBeanDefinitionException消息No qualifying bean of type [com.example.SomethingImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

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

Jen*_*ens 5

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

@Autowired
@Qualifier("SomethingImpl")
private Something _something;
Run Code Online (Sandbox Code Playgroud)