Home > Enterprise >  ApplicationContext not being recognized even after adding Maven dependency
ApplicationContext not being recognized even after adding Maven dependency

Time:11-15

I started a course on spring boot and I am at dependency injection. I ran into a problem with ApplicationContext which I can't seem to resolve: Screenshot of the problem

I added the maven dependency as you can see in the pom file: pom file screenshot

But it still doesn't recognize the ApplicationContext and I cannot find a solution anywhere.

EDIT: whole pom.xml file:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.6</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example</groupId>
  <artifactId>DependencyInjectionDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>DependencyInjectionDemo</name>
  <description>Demo project for Spring Boot</description>
  <properties>
    <java.version>11</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.12</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

CodePudding user response:

You don't need to explicitly add spring-context dependency, since it is a transitive dependency of spring-boot-starter. Other than that it seems fine to me, but we can do three things:

  1. Check that you are actually importing org.springframework.context.ApplicationContext and not some other class;
  2. Force the reload of all your dependencies in IntelliJ (right click your project --> Maven --> Reload project);
  3. Clear your .m2 folder (or at least org/springframework sub-path folder inside it). You might have some corrupted JARs in there.

CodePudding user response:

From given screenshots I assume, you are using a Maven-project in IntelliJ.

Adding imports from classpath

Try to import the class org.springframework.context.ApplicationContext in the java class where your main method is (as shown in screenshot).

Usually IntelliJ should find the class ApplicationContext in your classpath. From your given POM and dependency spring-boot-starter the classpath should contain the class.

See: ApplicationContext import in spring

Re-importing Maven dependencies

As João suggests try reimporting by context-action on your POM file (CTRL SHIFT O).

See:

Repair actions

In case nothing of the above work, either your IntelliJ project notconfigured correctly, or something got out of sync between IntelliJ and Maven. Then try following repair-actions (in order listed below):

  1. Run mvn clean install -U (see --update-snapshots), either from IntelliJ's Maven tools or terminal, as explained in Force re-download of release dependency using Maven

  2. In IntelliJ's menu select Invalidate Caches / Restart... to reindex your project. This can help if IntelliJ and Maven got out of sync.

  3. Delete your local Maven repository, usually the (hidden) folder .m2 in your user's home folder (see Clear the Maven Cache) and reload/reimport your POM in IntelliJ.

  • Related