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

前端 未结 26 2240
栀梦
栀梦 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 16:05

    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.

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

    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 at runtime.

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

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

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

    I solved that adding a Web facet.

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

    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.

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

    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.
    0 讨论(0)
提交回复
热议问题