IntelliJ IDEA 在使用 Spring 的 @Autowired 注释时显示错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21323309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:29:22  来源:igfitidea点击:

IntelliJ IDEA shows errors when using Spring's @Autowired annotation

springspring-mvcintellij-ideawarningsintellij-inspections

提问by Vainlyh

IntelliJ IDEA is showing errors when I use Spring's @Autowiredannotation in the class, but the class is functioning without any problem.

当我@Autowired在类中使用 Spring 的注释时,IntelliJ IDEA 显示错误,但该类运行没有任何问题。

Here is this error message:

这是此错误消息:

Autowired members must be defined in the valid spring bean (@Component/@Service,etc.) less... (Ctrl+F1) Checks autowiring problems in a bean class.

自动装配成员必须在有效的 spring bean(@Component/@Service 等)中定义 less... (Ctrl+F1) 检查 bean 类中的自动装配问题。

回答by Jose Leon

I had the same problem with IntelliJ IDEA 13.1.4 I solved it by removing the Spring facet (File->Project Structure) and leaving it to just show "Detection".

我在 IntelliJ IDEA 13.1.4 中遇到了同样的问题,我通过删除 Spring facet(文件-> 项目结构)并让它只显示“检测”来解决它。

回答by lanoxx

If you know that the bean exists and its just a problem of the inspections, then just add the following before the variable declaration:

如果您知道 bean 存在并且它只是检查的问题,那么只需在变量声明之前添加以下内容:

@SuppressWarnings("SpringJavaAutowiringInspection")
@Inject MyClass myVariable;

Sometimes IntelliJ cannot resolve if a bean has been declared, for example when the bean is included conditionally and the condition resolution happens at runtime.

有时 IntelliJ 无法解析 bean 是否已声明,例如当 bean 被有条件地包含并且条件解析发生在运行时。

回答by Thiago Pereira

Got the same error here!

这里有同样的错误!

It seems the Intellij cannot verify if the class implementation is a @Service or @Component.

Intellij 似乎无法验证类实现是@Service 还是@Component。

Solve it just changing from Error to Warning(Pressing Alt + Enter).

解决它只是从错误更改为警告(按 Alt + Enter)。

回答by user3580264

I fixed it by adding the supress warning:

我通过添加 supress 警告来修复它:

 @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
 @Autowired
 private ....

回答by Maciej

Remove .iml file from all your project module and next go to File -> Invalidate Caches/Restart

从所有项目模块中删除 .iml 文件,然后转到 File -> Invalidate Caches/Restart

回答by Aleksey Samoylov

File -> ProjectStructure -> Modules -> +(in central column) -> Spring ->OK

文件 -> ProjectStructure -> Modules -> +(在中间列) -> Spring ->OK

回答by jbarrameda

I had the same problem. I solved it by adding the Spring facet (File->Project Structure) for each relevant module, and then add the configuration files. For some projects (spring mvc), the config files where detected automatically. However, for a jar project, I had to add the configuration files manually.

我有同样的问题。我通过为每个相关模块添加 Spring facet(文件->项目结构)来解决它,然后添加配置文件。对于某些项目(spring mvc),会自动检测配置文件。但是,对于一个jar项目,我不得不手动添加配置文件。

回答by i-bob

Make sure you have your Spring bean definitions correct. Sometimes, the application works fine, it just displays an error in the IDE, check your project ‘iml' file if you have a Spring facet defined.

确保您的 Spring bean 定义正确。有时,应用程序工作正常,它只是在 IDE 中显示错误,如果您定义了 Spring facet,请检查您的项目“iml”文件。

回答by tomcyjohn

Solved the issue by going to File >> Project Structure >> Facets and then adding all the configuration files to Spring Facet. After that it started detecting files in which the beans reside and was able to sort the issue. IntelliJ giving this check is quite valuable and IMHO shouldn't be disabled.

通过转到 File >> Project Structure >> Facets 然后将所有配置文件添加到 Spring Facet 解决了该问题。之后,它开始检测 bean 所在的文件并能够对问题进行排序。IntelliJ 提供此检查非常有价值,恕我直言,不应禁用。

回答by vivekmore

Make sure that your IntelliJ Idea (IDE) is aware of all the necessary spring configurations that your module is being inspected against.

确保您的 IntelliJ Idea (IDE) 了解正在检查您的模块的所有必要弹簧配置。

You can check this under

你可以在下面检查

File > Project Structure > Modules > [your project name in the right panel] > Spring

文件 > 项目结构 > 模块 > [右侧面板中的项目名称] > Spring

Sometimes, we need to explicitly tell the IDE that the spring configuration is coming from a dependency (a jar present in your project classpath)

有时,我们需要明确地告诉 IDE spring 配置来自依赖项(项目类路径中存在的 jar)