Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Why Autowired is not working?

When @Autowired doesn't work. There are several reasons @Autowired might not work. When a new instance is created not by Spring but by for example manually calling a constructor, the instance of the class will not be registered in the Spring context and thus not available for dependency injection.

What is the use of * Autowired * annotation?

The @Autowired annotation provides more fine-grained control over where and how autowiring should be accomplished. The @Autowired annotation can be used to autowire bean on the setter method just like @Required annotation, constructor, a property or methods with arbitrary names and/or multiple arguments.

Should I use inject or Autowired?

@Inject and @Autowired both annotations are used for autowiring in your application. @Inject annotation is part of Java CDI which was introduced in Java 6, whereas @Autowire annotation is part of spring framework. Both annotations fulfill same purpose therefore, anything of these we can use in our application.


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".


I fixed it by adding the supress warning:

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

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

@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 dynamically at runtime. In such a case it seems the static code analyzer of IntelliJ cannot detect the bean.


Got the same error here!

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

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


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


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


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.