Java Spring Boot:自动装配具体类时“找不到类型的合格bean...找到”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28895248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:00:10  来源:igfitidea点击:

Spring Boot: "No qualifying bean of type... found" when autowiring concrete class

javaspringhibernatespring-mvcspring-boot

提问by rabejens

I am writing a component using Spring Boot and Spring Boot JPA. I have a setup like this:

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

The interface:

界面:

public interface Something {
    // method definitions
}

The implementation:

实施:

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

Now, I have a JUnit test which runs with SpringJUnit4ClassRunner, and I want to test my SomethingImplwith this.

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

When I do

当我做

@Autowired
private Something _something;

it works, but

它有效,但是

@Autowired
private SomethingImpl _something;

causes the test to fail throwing a NoSuchBeanDefinitionExceptionwith the message 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)}

导致测试失败并抛出NoSuchBeanDefinitionException带有消息的 aNo 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)}

But in the test case, I want to explicitlyinject my SomethingImplbecause it is the class I want to test. How to I achieve this?

但是在测试用例中,我想显式注入 mySomethingImpl因为它是我要测试的类。我如何实现这一目标?

采纳答案by Jens

If you want a special bean you have to use the @Qualifierannotation:

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

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

回答by rabejens

I figured out you can do the same with a javax.injectstyle DI:

我发现你可以用javax.inject样式 DI做同样的事情:

@Named("myConcreteThing")
public class SomethingImpl implements Something { ... }

Where you want to inject it:

你想注入它的地方:

@Inject
@Named("myConcreteThing")
private Something _something;

This is correctly picked up by @EnableAutoConfigurationand @ComponentScan.

这是由@EnableAutoConfiguration和正确拾取的@ComponentScan

回答by Ravi Macha

I think you need to add @Service in implementing class.. like

我认为您需要在实现类中添加 @Service .. 喜欢

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

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

回答by Ramzi Guetiti

I had the same issue and I could resolve the issue by adding component scan path to the Application class as below:

我遇到了同样的问题,我可以通过向 Application 类添加组件扫描路径来解决这个问题,如下所示:

@ComponentScan(basePackages= {"xx.xx"})