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

没有豆子?

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


当前回答

在存储库类上添加Spring注释@Repository。

我知道没有这个注释它也能工作。但是如果添加了这个,IntelliJ将不会显示错误。

@Repository
public interface YourRepository ...
...

如果您将Spring Data与扩展Repository类一起使用,则会产生冲突包。那么你必须直接标明包裹。

import org.springframework.data.repository.Repository;
...

@org.springframework.stereotype.Repository
public interface YourRepository extends Repository<YourClass, Long> {
    ...
}

接下来,您可以自动连接存储库而不会出现错误。

@Autowired
YourRepository yourRepository;

这可能不是一个好的解决方案(我猜您试图注册存储库两次)。但是为我工作,不要出错。

也许在IntelliJ的新版本中可以修复:https://youtrack.jetbrains.com/issue/IDEA-137023

其他回答

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

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

在使用@SpringBootApplication注释创建SpringBoot应用程序时,我也遇到了同样的问题。该注释根据spring引用表示@Configuration、@EnableAutoConfiguration和@ComponentScan。

正如预期的那样,新的注释工作正常,我的应用程序运行顺利,但Intellij一直抱怨未实现的@Autowire依赖项。当我重新分别使用@Configuration、@EnableAutoConfiguration和@ComponentScan时,错误就停止了。Intellij 14.0.3(很可能是更早的版本)似乎还没有配置为识别@SpringBootApplication注释。

现在,如果错误对您的影响如此之大,那么就恢复到那三个单独的注释。否则,忽略Intellij…您的依赖项解析配置正确,因为您的测试通过了。

永远记得……

人永远比机器伟大。

作为IntelliJ (IDE)和开发环境之间的大多数同步错误。

特别是如果您有自动测试或构建,通过绿色始终。

无效缓存和重新启动解决了我的问题。

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

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

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

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

或者选择Eclipse:)