0

I am learning Spring and trying to practice the example from a book.

in pom.xml i have used dependency as below. while i am using the annotations and classes which are related to spring-context, it says error can not resolve the values (@Configuration, @Bean, AnnotaionConfigApplicationContext)

Please suggest how to resolve and move ahead.

in pom.xml itself the version tag is with error

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>SpringLearning</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.6.RELEASE</version> !-- this is RED highlighted "dependency not found" 
        </dependency>
    </dependencies>

</project>

Classes

@Configuration
public class MyConfig {

    @Bean
    public Parrot parrot() {
        var p = new Parrot();
        p.setName("abc");
        return p;
    }
}

main class

public class Main {

    public static void main(String[] args) {
        var context = new AnnotaionConfigApplicationContext();
        Parrot p = new Parrot();
    }
}

public class Parrot {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

I tried changing the versions like 6.0.3 referring from spring.io but similar error. tried searching the resolution online but no clues yet hence writing here.

Keyvan Soleimani
  • 606
  • 2
  • 4
  • 16

1 Answers1

0

I am able to resolve the errors by following below steps

  1. Right click on pom.xml -> maven->Reload Project also i have tried (Load Maven Changes option) with other dependencies
  2. Added imports import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;

with these steps no more error now.

Learning: Need to properly add imports and dependencies for spring projects