intellij错误地说没有为自动装配的存储库找到类型的bean

Rob*_*_UK 117 java spring annotations intellij-idea autowired

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

没豆子?

在此输入图像描述

如下所示,它通过了测试?那么必须是Autowired?

在此输入图像描述

Abi*_*oso 151

在使用注释创建Spring Boot应用程序时,我遇到了同样的问题@SpringBootApplication.这个注释代表@Configuration,@EnableAutoConfiguration@ComponentScan根据春季参考.

正如所料,新的注释工作正常,我的应用程序运行顺利但是,Intellij一直在抱怨未实现的@Autowire依赖项.一旦我改回使用@Configuration,@EnableAutoConfiguration并且@ComponentScan单独地,错误就停止了.似乎Intellij 14.0.3(很可能也是早期版本)尚未配置为识别@SpringBootApplication注释.

现在,如果错误打扰你那么多,那么回到那三个单独的注释.否则,忽略Intellij ...您的依赖项解析已正确配置,因为您的测试通过了.

总记得...

人永远比机器大.

  • 我仍然使用2017.2获得它,它是最终许可证的付款. (35认同)
  • 与 2020.3、Spring Boot 2.3.7、Kotlin 1.4.21 相同,对于 @Autowired Lateinit varEmbeddedKafka:EmbeddedKafkaBroker (6认同)
  • 遗憾的是,这种情况在 2022.2 终极版中仍然发生 (6认同)
  • 对于更新版本的IntelliJ(例如2017),您必须启用Spring Data插件,然后您不需要任何上述解决方法.下面还有另一个答案告诉你如何做到这一点. (4认同)
  • 使用IntelliJ 2018.2,使用Spring Boot 2.0.5和`@ SpringBootApplication`,我收到了这个错误.我遵循@JaõsMatos的建议,将`scanBasePackages`参数用于`@SpringBootApplication`并指定应扫描的包/命名空间. (4认同)
  • 这里同样,版本2021.1.2 旗舰版 (2认同)
  • 存在于2022.1终极版 (2认同)
  • 仍然向我显示错误[IntelliJ IDEA 2023.1.2(终极版)] (2认同)

mkc*_*zyk 46

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

我知道它应该没有这个注释.但是如果你添加它,IntelliJ将不会显示错误.

@Repository
public interface YourRepository ...
...
Run Code Online (Sandbox Code Playgroud)

如果你使用Spring Data扩展Repository类,那将是冲突的问题.然后你必须指出明确的pagkages.

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

@org.springframework.stereotype.Repository
public interface YourRepository extends Repository<YourClass, Long> {
    ...
}
Run Code Online (Sandbox Code Playgroud)

接下来,您可以无错误地自动装配您的存储库.

@Autowired
YourRepository yourRepository;
Run Code Online (Sandbox Code Playgroud)

它可能不是一个好的解决方案(我猜你试图两次注册repositorium).但为我工作,不要显示错误.

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

  • 三年后又坏了 (8认同)
  • 根据这个https://youtrack.jetbrains.com/issue/IDEA-148113,修复版本16 ...(保持呼吸) (3认同)

Nat*_*tix 24

我的IntelliJ IDEA Ultimate版本(2016.3.4 Build 163)似乎支持这一点.诀窍是你需要启用Spring Data插件.

在此输入图像描述

  • 在2017.2.7不起作用.插件已启用,但警告仍然存在. (5认同)
  • 即使启用插件后也不起作用 (4认同)

Joã*_*tos 19

有时您需要指示@ComponentScan应扫描组件的位置.您可以通过将包作为此注释的参数传递来执行此操作,例如:

@ComponentScan(basePackages={"path.to.my.components","path.to.my.othercomponents"})
Run Code Online (Sandbox Code Playgroud)

但是,如前所述,@ SpringBootApplication注释替换了@ComponentScan,因此在这种情况下,您必须执行相同的操作:

@SpringBootApplication(scanBasePackages={"path.to.my.components","path.to.my.othercomponents"})
Run Code Online (Sandbox Code Playgroud)

至少在我的情况下,Intellij停止了抱怨.

  • 这绝对是更好,更清洁的解决方案,并且可以确认这消除了警告.谢谢! (2认同)
  • 就我而言,我有 `@SpringBootApplication(scanBasePackages={"com.ab, com.ac"})`,虽然该应用程序运行良好,但 Intellij 不喜欢它。更改为`@SpringBootApplication(scanBasePackages={"com.ab", "com.ac"})` 已为我修复! (2认同)
  • @GriffoGoes 的建议解决了我的问题,因为我使用的是多模块结构,这是我最好的解决方案 (2认同)

cri*_*ess 17

我总是通过以下方式解决这个问题. 设置>检查> Spring Core>代码,而不是从错误转移到警告严重性选项

在此输入图像描述

  • 在 IntelliJ 2021.3 中,此设置已重命名为“Spring bean 组件中的注入点自动装配不正确”。 (5认同)
  • 我一直使用此解决方案,直到在Spring上完成修复为止:https://youtrack.jetbrains.com/issue/IDEA-182912 (2认同)

小智 14

我使用的是spring-boot 2.0和intellij 2018.1.1终极版,我遇到了同样的问题.

我通过将@EnableAutoConfiguration放在主应用程序类中来解决

@SpringBootApplication
@EnableAutoConfiguration
class App{
/**/
}
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说也"有效",但是intellij抱怨说:`冗余声明:@SpringBootApplication已经应用了@EnableAutoConfiguration`¯\\ _(ツ)_ /¯ (9认同)
  • 同样在这里。冗余声明错误:/ (3认同)

小智 12

检查您是否错过了服务类中的 @Service 注释,我就是这种情况。


小智 12

@Service您是否检查过您是否在服务实现之上使用了注释?这对我有用。

import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserServices {}
Run Code Online (Sandbox Code Playgroud)


小智 7

配置应用程序上下文,一切都会好起来的。

在此处输入图片说明


小智 7

简单,您只需执行 2 个步骤

  1. 添加 hibernate-core 依赖
  2. 改成。@Autowired@Resource
==>> change @Autowired to  @Resource
Run Code Online (Sandbox Code Playgroud)

  • 解释你的答案。 (2认同)

小智 6

在类级别使用@EnableAutoConfiguration注释@Component。它将解决这个问题。

例如:

@Component
@EnableAutoConfiguration  
public class ItemDataInitializer  {

    @Autowired
    private ItemReactiveRepository itemReactiveRepository;

    @Autowired
    private MongoOperations mongoOperations;
}
Run Code Online (Sandbox Code Playgroud)


wir*_*d00 5

@Component或者@configuration在bean的配置文件似乎工作,像IE的东西:

@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");
    }
}
Run Code Online (Sandbox Code Playgroud)


Xia*_*com 5

如果您不想仅仅为了让您的 IDE 满意而对代码进行任何更改。我通过将所有组件添加到 Spring 方面解决了这个问题。

  1. 创建一个名为“服务、处理器和路由器”或任何您喜欢的名称的组;
  2. 删除并重新创建“Spring 应用程序上下文”,使用您之前创建的组作为父组。

在此输入图像描述


ima*_*era 5

只要您的测试通过了,您就可以了,alt + enter将光标放在错误上,然后在第一个项目的子菜单中选择Disable Inspection该项目


小智 5

当此错误出现在 IntelliJ v.14 中时,我使用此注释来隐藏此错误:

@SuppressWarnings("SpringJavaAutowiringInspection")
Run Code Online (Sandbox Code Playgroud)

  • 当自动装配到构造函数中时,它是`@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")` (3认同)

小智 5

对我来说,解决方案是将它放在@EnableAutoConfigurationApplication 类中@SpringBootApplication,因为它是多余的,所以要给它下划线。删除它,瞧,所有关于丢失豆子的警告都消失了!愚蠢的春天...