简体   繁体   English

IntelliJ 和 Spring 启动测试:未解析的 bean,无法自动装配

[英]IntelliJ and Spring boot test: unresolved bean, could not autowire

I have got a gradle imported project in IntelliJ, kotlin.我在 IntelliJ 中有一个 gradle 导入项目,kotlin。 Compilations go fine, tests are run fine both via command line and via IntelliJ.编译 go 很好,测试通过命令行和 IntelliJ 都可以正常运行。 The only thing I'd like gone is the red error highlighting saying eg "Could not autowire. No beans of 'FooRepository' type found".我唯一想要的是红色错误突出显示例如“无法自动装配。没有找到'FooRepository'类型的bean”。 The weird part is that I've got exact same setup in another PC and there it works fine.奇怪的是,我在另一台 PC 上进行了完全相同的设置,并且工作正常。 Using IntelliJ 2021.1 on Ubuntu.在 Ubuntu 上使用 IntelliJ 2021.1 I can't recall this in the previous versions.在以前的版本中我不记得了。

在此处输入图像描述

The FooRepository is declared with @Repository annotation, like this: FooRepository使用@Repository注释声明,如下所示:

@Repository
class FooTypeRepository(
    @Autowired var jdbcTemplate: NamedParameterJdbcTemplate
){...}

I had same issue, resolved it by deleting .idea folder and reimporting the project.我有同样的问题,通过删除.idea文件夹并重新导入项目来解决它。 Sometimes IntelliJ just need to reimport and reindex everything.有时 IntelliJ 只需要重新导入和重新索引所有内容。

Your class FooRepository needs to be a Spring Bean.您的 class FooRepository 需要是 Spring Bean。 It has to have some annotation indicating that it's a bean.它必须有一些注释表明它是一个 bean。 For example annotation @Repository or just @Component.例如注释@Repository 或只是@Component。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM