Why annotation cannot be resolved despite proper import?

I have maven project with spring dependency successfully added. Component annotation is resolved when used, but Autowired annotation generates error, despite that it appears on the build path. How do i fix this?

enter image description here

first component class

import org.springframework.stereotype.Component;

@Component
public class Comp1 {
}

second component class

import org.springframework.stereotype.Component;
//error: The import org.springframework.beans.factory.annotation.Autowired cannot be resolved
import org.springframework.beans.factory.annotation.Autowired;

@Component
public class Comp2 {
    @Autowired // error: Autowired cannot be resolved to a type
    private Comp1 comp1;

    public Comp1 getComp1() {
        return comp1;
    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Spr</groupId>
  <artifactId>Spr</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.0.5.RELEASE</version>
    </dependency>
  </dependencies>

</project>

Answer

Deletion of local maven repository folder (.m2) and redownloading it, solved the problem.

Leave a Reply

Your email address will not be published. Required fields are marked *