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

没有豆子?

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


当前回答

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注释也可以工作。)

其他回答

IntelliJ IDEA Ultimate

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

文件->项目结构..

左边: 项目设置->模块

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

只要你的测试通过了,你是好的,点击alt + enter,将光标移到错误上,在第一项的子菜单中,你会发现禁用检查,选择它

如果你不想对你的代码做任何改变,只是为了让你的IDE满意。我通过将所有组件添加到Spring facet解决了这个问题。

创建一个名为“服务,处理器和路由器”的组或任何你喜欢的名称; 删除并重新创建“Spring Application Context”,使用之前创建的组作为父组。

在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");
    }
}

你需要做的就是添加

AppConfiguration.java中的@ComponentScan("package/include/your/annotation/component")

因为我认为你的AppConfiguraion.java的包比你的注释组件(@Service, @Component…)的包更深,

比如“package/include/your/annotation/component/deep /config”。