IntelliJ IDEA shows errors when using Spring's @Autowired annotation
当我在类中使用Spring的
这是此错误消息:
Autowired members must be defined in the valid spring
bean (@Component/@Service,etc.) less... (Ctrl+F1) Checks autowiring
problems in a bean class.
我在IntelliJ IDEA 13.1.4中遇到了相同的问题
我通过删除Spring构面("文件"->"项目结构")并将其保留为仅显示"检测"来解决了该问题。
如果您知道该bean存在并且只是检查的问题,那么只需在变量声明之前添加以下内容:
1 2 | @SuppressWarnings("SpringJavaAutowiringInspection") @Inject MyClass myVariable; |
有时IntelliJ无法解析是否已声明Bean,例如,有条件地包含该Bean且条件解析在运行时发生。
在这里遇到了同样的错误!
看来Intellij无法验证类实现是@Service还是@Component。
解决它只是从错误更改为警告(按Alt + Enter)。
从所有项目模块中删除.iml文件,然后转到File-> Invalidate Caches / Restart
我通过添加禁止警告来解决此问题:
1 2 3 | @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") @Autowired private .... |
文件-> ProjectStructure->模块-> +(在中心列)-> Spring-> OK
我有同样的问题。我通过为每个相关模块添加Spring facet("文件"->"项目结构")来解决此问题,然后添加配置文件。对于某些项目(spring mvc),将自动检测配置文件。但是,对于jar项目,我必须手动添加配置文件。
确保您的Spring bean定义正确。有时,应用程序运行良好,仅在IDE中显示错误,如果定义了Spring facet,请检查项目的" iml"文件。
通过转到文件>>项目结构>>构面,然后将所有配置文件添加到Spring Facet,解决了该问题。
之后,它开始检测Bean所在的文件,并能够对问题进行排序。
IntelliJ给予此检查非常有价值,不应禁用IMHO。
似乎是可见性问题-父控制器看不到您要连接的组件。
尝试添加
1 | @ComponentScan("path to respective Component") |
到父控制器。
确保您的IntelliJ Idea(IDE)知道要检查其模块的所有必要弹簧配置。
您可以在下面检查
File > Project Structure > Modules > [your project name in the right panel] > Spring
有时,我们需要明确地告诉IDE,spring配置来自于一个依赖项(项目类路径中存在一个jar)
您应该检查是否在类上添加了@ Component,@ Repository或类似内容
就我而言,我缺少在web.xml中编写的信息:
1 2 3 4 5 6 7 8 9 10 11 12 | <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> |
并在应用程序上下文文件中:
1 | <context:component-scan base-package=[your package name] /> |
添加此标签并运行maven来重建项目后,Intellij Disapears中的自动装配错误和Bean图标出现在左边缘:
我也有这个问题。
进行alt + enter,然后要求重新运行或在修复它的受影响行上禁用Spring检查。
在13.4更新之后,这似乎只是一个问题。
我遇到了同样的问题。我的原因是因为包含自动连接的引用的bean不是Spring组件(它是EJB),而是得到了一个SpringBeanAutowiringInterceptor Interceptor,可以使用自动装配。我认为Intellij在其自动装配检查中不会采取这种可能性。
我的原因是没有在CrudRepository界面上添加@Repository,我正在观看的教程没有在STS上添加它,因此也没有抱怨。
我知道这是一个老问题,但是我没有找到能为我解决该问题的任何答案,因此我将提供解决方案。
注意:我认为问题可能出在此,但是我的问题与两次实现同一接口无关。使用
背景
我的任务是维护一个旧项目,该项目经历了不同版本的spring,并且仅针对单独的模块进行了更新,因此至少可以说需要重构。我最初得到了重复的bean问题,并且通过修补问题在OP的问题和重复的bean问题之间来回改变了问题,即使只有一个bean也是如此。导航到重复的bean总是去同一个类。
问题
此问题出现在
简单的解决方案
由于已经实现了使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 { |
我已经通过这种方式解决了这个问题。
在IntelliJ中,您的所有软件包都应位于main / java的子软件包中。
例如,我将所有软件包都放在src / main / java / com.misisol.watchStore /下,spring之后可以找到我的bean。
使用@Qualifier注入Bean为我解决了这个问题。
我有类似的问题。我通过取消选中"处理显式注释的bean"选项来解决它(请参见下面的屏幕截图)。在Linux上默认启用此选项。
现在,@ Service和@Configurations批注可见。
屏幕截图
我解决了添加Web方面的问题。
有点晚了,但我希望对别人有帮助。
确保将@Service放在服务的实现类上
1 2 3 4 5 6 7 8 9 | @Service public class ServiceNameImpl implements ServiceName { @Override public void method(ObjectType paramName) { //CODE } } |
这就是我修复错误的方式。