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

前端 未结 26 2242
栀梦
栀梦 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:53
    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 {
    
    0 讨论(0)
  • 2020-12-07 15:55

    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.

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

    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.

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

    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.

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

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

    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.

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