我已经创建了一个简单的单元测试,但是IntelliJ错误地将它突出显示为红色。将其标记为错误

没有豆子?

如你所见,它通过了测试?所以它一定是自动连接的?


当前回答

我在spring引导应用程序中解决这个问题的方法是打开spring应用程序上下文,并手动为缺失的自动连接bean添加类!

(通过项目结构菜单或弹簧工具窗口访问…编辑“Spring Application Context”)

因此,SpringApplicationContext不仅仅包含我的ExampleApplication spring配置,它还包含了缺失的Bean:

SpringApplicationContext:

ExampleApplication.java MissingBeanClass.java

et voilà:错误信息消失!

其他回答

我只能使用@EnableAutoConfiguration来解决它,但是这个错误没有功能上的影响。

令人惊讶的是,一个在Eclipse中成功运行的面向佯装的项目不能在InteliJ中运行。 当启动应用程序时,InteliJ抱怨我试图注入到serviceImpl层的Feign客户端说: 字段personRestClient(我的虚拟客户端)在…需要一个豆子类型…但没有找到。 考虑定义一个‘....’类型的bean在你的配置中。

我浪费了很长时间试图弄清楚是怎么回事。 我发现了一个解决方案(InteliJ),我不完全理解:

Alt Shift F10(或运行菜单) 选择“编辑配置” 在配置窗口中,选中复选框的“包含依赖项” “提供”范围的 运行应用程序

或者选择Eclipse:)

I had similar issue in Spring Boot application. The application utilizes Feign (HTTP client synthetizing requests from annotated interfaces). Having interface SomeClient annotated with @FeignClient, Feign generates runtime proxy class implementing this interface. When some Spring component tries to autowire bean of type SomeClient, Idea complains no bean of type SomeClient found since no real class actually exists in project and Idea is not taught to understand @FeignClient annotation in any way.

解决方案:用@Component注解接口SomeClient。(在我们的例子中,我们没有直接在SomeClient上使用@FeignClient注释,而是使用metaannotation @OurProjectFeignClient,它是带注释的@FeignClient,并向其添加@Component注释也可以工作。)

在bean配置文件中放置@Component或@configuration似乎可以工作,例如:

@Configuration
public class MyApplicationContext {
    @Bean
    public DirectoryScanner scanner() {
        return new WatchServiceDirectoryScanner("/tmp/myDir");
    }
}

@Component
public class MyApplicationContext {
    @Bean
    public DirectoryScanner scanner() {
        return new WatchServiceDirectoryScanner("/tmp/myDir");
    }
}

IntelliJ IDEA Ultimate

将您的主类添加到IntelliJ Spring应用程序上下文中,例如Application.java

文件->项目结构..

左边: 项目设置->模块

右边:在你的包结构中找到 Spring并添加+ Application.java