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

前端 未结 26 2376
栀梦
栀梦 2020-12-07 15:40

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

26条回答
  •  执笔经年
    2020-12-07 15:59

    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.

提交回复
热议问题