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

SpringSpring MvcIntellij IdeaWarningsIntellij Inspections

Spring Problem Overview


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

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 Solutions


Solution 1 - Spring

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

Solution 2 - Spring

I fixed it by adding the supress warning:

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

Solution 3 - Spring

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.

Solution 4 - Spring

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

Solution 5 - Spring

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

Solution 6 - Spring

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

Solution 7 - Spring

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.

Solution 8 - Spring

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.

Solution 9 - Spring

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.

Solution 10 - Spring

I have the same issue.

enter image description here

And I think the proper way to fix it is to tell Intellij to find the correct Spring Context, rather than "suppress warning".

In short, File -> ProjectStructure(⌘;) -> Modules -> (select the module) -> Spring -> (click '+' to add context) -> OK

enter image description here

Add the context file:

enter image description here

done!

enter image description here

Solution 11 - Spring

I might be a little late, but after spending hours and researching on this issue.

I found out that in the latest version IntelliJ 2020 @AutoWired is optional and constructor based depedency injection is preferable.

I solved the problem by simply removing the @AutoWired Annotation from Service and Controller class and using constructor based dependency injection.

This link might help.

Happy Coding!

Solution 12 - Spring

It seems like the visibility problem - the parent controller doesn't see the Component you are trying to wire.

Try to add

@ComponentScan("path to respective Component") 

to the parent controller.

Solution 13 - Spring

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

You can check this under

> File > Project Structure > Modules > [your project name in the right panel] > 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)

Solution 14 - Spring

I got the same problem. Mine was because the bean containing the autowired reference was not a Spring component (it was an EJB), but got a SpringBeanAutowiringInterceptor Interceptor allowing the use of autowiring. I think Intellij don't take this possibility in its Autowiring inspection.

Solution 15 - Spring

I had this problem too. Doing alt+enter and then asking to either re-run or disable Spring inspection on the effected line fixed it. This only seems to have become an issue after the 13.4 update.

Solution 16 - Spring

in my case I was missing to write in 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>

and in the application context file:

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

after add this tags and run maven to rebuild project the autowired error in intellj desapears and the bean icon appears in the left margin: enter image description here

Solution 17 - Spring

Mine is for not adding @Repository on my CrudRepository interface, the tutorial I was watching didn't add it on STS and it didn't complain.

Solution 18 - Spring

You should check if you have @Component, @Repository or similar added on the class

Solution 19 - Spring

a little late but i hope it helps to someone else.

Make sure to put the @Service on the implementation class for the service

@Service
public class ServiceNameImpl implements ServiceName {

    @Override
    public void method(ObjectType paramName) {
        //CODE
    }

}

That's how i fixed the error.

Solution 20 - Spring

I know this is an old question, but I haven't come across any answers that solved this problem for me so I'll provide my solution.

Note: I thought the issue may have been this, but my issue wasn't related to implementing the same interface twice. Using @Qualitier did make my issue go away, but it was a bandage and not a proper solution so I didn't settle with that.

BACKGROUND

I'm tasked with maintaining an old project that has gone through different versions of spring and only updated for separate modules, so things needed refactoring, to say the least. I had initially gotten the duplicate bean issue and tinkering with things changed the issue back and forth between OP's issue and the duplicate bean issue even though there was only one bean; navigating to the duplicate beans always went to the same class.

THE ISSUE

The issue was present on a @Repository class that was @Autowired in a @Service class which was also had the @ComponentScan annotation. I noticed that I also had a spring application-config.xml that was doing a context:component-scan on the base package, which I believe was the original approach in older versions of Spring. I was in the process of making a new branch by taking parts of an old branch and a newer branch in a support project that was used in different projects that were developed over several years and that is why there was such a mix-and-match of methodologies.

SIMPLE SOLUTION

Since the more modern approach of using @ComponentScan was already implemented I just removed the application-config.xml and the issue was solved.

Solution 21 - Spring

I solved that adding a Web facet.

Solution 22 - Spring

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 {

Solution 23 - Spring

I've solved this problem this way. In IntelliJ all of your packages should be in a sub package which is the sub package of main/java. For example I've put all of my packages under src/main/java/com.misisol.watchStore/ and spring could find my beans then after.

Solution 24 - Spring

Inject Bean with @Qualifier solved the problem for me.

Solution 25 - Spring

I had similar problem. I solved it by unchecking "Process explicitly annotated beans" option (see screenshot below). This option is enabled by default on linux. Now @Service and @Configurations annotations are visible. screenshot

Solution 26 - Spring

The following worked for me:

  1. Find all classes implementing the service(interface) which is giving the error.
  2. Mark each of those classes with the @Service annotation, to indicate them as business logic classes.
  3. Rebuild the project.

Solution 27 - Spring

I had this problem with only one service with constructor based dependency injection with 2019.2.4 version of IntelliJ. I found it helpful to change the name of the service (shift + f6) and then discard the changes from the git level.

Solution 28 - Spring

For those who are using IDEA, you can simply remove those warnings that you know.

In mac, move your curse to the red wave line, press option+enter(alt+enter with Windows), you will see suggestions:

enter image description here

Try them, and you will see auto-added SuppressWarnings

Remember, option+enter always give nice solutions.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVainlyhView Question on Stackoverflow
Solution 1 - SpringJose LeonView Answer on Stackoverflow
Solution 2 - Springuser3580264View Answer on Stackoverflow
Solution 3 - SpringlanoxxView Answer on Stackoverflow
Solution 4 - SpringThiago PereiraView Answer on Stackoverflow
Solution 5 - SpringMaciejView Answer on Stackoverflow
Solution 6 - SpringAleksey SamoylovView Answer on Stackoverflow
Solution 7 - SpringjbarramedaView Answer on Stackoverflow
Solution 8 - SpringtomcyjohnView Answer on Stackoverflow
Solution 9 - Springi-bobView Answer on Stackoverflow
Solution 10 - Springkarl liView Answer on Stackoverflow
Solution 11 - SpringShariq ShaikhView Answer on Stackoverflow
Solution 12 - SpringEugene KarasevView Answer on Stackoverflow
Solution 13 - SpringvivekmoreView Answer on Stackoverflow
Solution 14 - SpringTeocaliView Answer on Stackoverflow
Solution 15 - SpringJason DView Answer on Stackoverflow
Solution 16 - SpringdemianView Answer on Stackoverflow
Solution 17 - SpringRejinderiView Answer on Stackoverflow
Solution 18 - SpringMilos NikolikView Answer on Stackoverflow
Solution 19 - SpringMar VilleneuveView Answer on Stackoverflow
Solution 20 - SpringDarrel HoltView Answer on Stackoverflow
Solution 21 - SpringPavel VlasovView Answer on Stackoverflow
Solution 22 - SpringleeView Answer on Stackoverflow
Solution 23 - SpringMehrdad SharifiView Answer on Stackoverflow
Solution 24 - SpringPonleuView Answer on Stackoverflow
Solution 25 - SpringAleksander BurzecView Answer on Stackoverflow
Solution 26 - SpringtonderaimuchadaView Answer on Stackoverflow
Solution 27 - SpringVodzooView Answer on Stackoverflow
Solution 28 - SpringcinqSView Answer on Stackoverflow