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

前端 未结 26 2243
栀梦
栀梦 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:57

    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.

    0 讨论(0)
  • 2020-12-07 15:58

    Inject Bean with @Qualifier solved the problem for me.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-07 16:00

    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.

    0 讨论(0)
  • 2020-12-07 16:00

    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.

    0 讨论(0)
  • 2020-12-07 16:02

    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:

    0 讨论(0)
提交回复
热议问题