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

问题描述 投票:83回答:22

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

这是以下错误消息:

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

spring spring-mvc intellij-idea warnings intellij-inspections
22个回答
29
投票

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


2
投票

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


1
投票

我遇到了同样的问题。我的原因是包含autowired引用的bean不是Spring组件(它是一个EJB),但得到了SpringBeanAutowiringInterceptor Interceptor,允许使用自动装配。我认为Intellij在其自动装配检查中没有采用这种可能性。


1
投票

我也有这个问题。执行alt +输入然后要求重新运行或禁用受影响线上的弹簧检查修复它。这似乎只是在13.4更新后成为一个问题。


1
投票

这似乎是可见性问题 - 父控制器没有看到您尝试连接的组件。

尝试添加

@ComponentScan("path to respective Component") 

到父控制器。


1
投票

在我的情况下,我不想在web.xml中写:

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:applicationContext.xml</param-value>
   </context-param>

并在应用程序上下文文件中:

<context:component-scan base-package=[your package name] />

添加此标签并运行maven重建项目后,intellij中的自动装配错误消失,bean图标显示在左边距:qazxsw poi


1
投票

我的目的是不在我的CrudRepository界面上添加@Repository,我正在观看的教程没有在STS上添加它并且它没有抱怨。


1
投票

您应该检查是否在类上添加了@ Component,@ Repository或类似内容


0
投票

我解决了添加Web方面的问题。


0
投票
enter image description here

0
投票

我用这种方式解决了这个问题。在IntelliJ中,所有包都应该在子包中,这是main / java的子包。例如,我将所有的软件包放在src / main / java / com.misisol.watchStore /下,然后spring可以找到我的bean。


17
投票

在这里得到了同样的错误!

看来Intellij无法验证类实现是@Service还是@Component。

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


0
投票

使用@Qualifier注入Bean解决了我的问题。


0
投票

我有类似的问题。我通过取消选中“Process explicit annotated beans”选项解决了这个问题(见下面的截图)。默认情况下,在linux上启用此选项。现在可以看到@Service和@Configurations注释。 eg1: director:Settings - Editor - Inspections - Spring - Spring Core - Code - Autowiring for Bean Class operate:checkout 勾去掉 eg2: 1.impl class add @service like this: @Service public class CityServiceImpl implements CityService{ @Autowired private CityDao cityDao; like this 2.dao file class add @Repository @Repository public interface CityDao {


0
投票

有点晚了,但我希望它对其他人有帮助。

确保将@Service放在服务的实现类上

screenshot

这就是我如何修复错误。


17
投票

从所有项目模块中删除.iml文件,然后转到文件 - >无效缓存/重新启动


17
投票

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

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

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


13
投票

文件 - > ProjectStructure - >模块 - > +(在中央列) - > Spring - > OK


13
投票

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

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

5
投票

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


4
投票

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


2
投票

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

你可以在下面查看

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

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

© www.soinside.com 2019 - 2024. All rights reserved.