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
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.
Inject Bean with @Qualifier solved the problem for me.
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.
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.
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.
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: