我开始了一门关于spring boot的课程,我正在学习依赖注入。我遇到了一个似乎无法解决的ApplicationContext问题:Screenshot of the problem
我添加了maven依赖项,如您在pom文件中所见:pom file screenshot
但它仍然不能识别ApplicationContext,我在任何地方都找不到解决方案。
编辑:整个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 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>发布于 2021-11-14 22:59:07
您不需要显式添加spring-context依赖,因为它是spring-boot-starter的传递依赖。除此之外,对我来说似乎没问题,但我们可以做三件事:
org.springframework.context.ApplicationContext而不是其他类;.m2文件夹(或至少其中的org/springframework子路径文件夹)。你可能有一些坏了的罐子。发布于 2021-11-14 23:19:07
根据给定的屏幕截图,我假设您正在使用IntelliJ中的Maven-project。
从类路径添加导入
尝试在您的main方法所在的java类中导入org.springframework.context.ApplicationContext类(如截图所示)。
通常,IntelliJ应该在类路径中找到类ApplicationContext。根据给定的POM和依赖项spring-boot-starter,类路径应该包含类。
请参阅:ApplicationContext import in spring
重新导入Maven依赖关系
以João suggests身份尝试对POM文件执行上下文操作(CTRL + SHIFT + O)。
请参见:
修复操作
如果上面的任何一个都不起作用,要么是你的IntelliJ项目配置不正确,要么是IntelliJ和Maven之间的某些东西不同步。然后尝试以下修复操作(按下面列出的顺序):
mvn clean install -U (请参阅--update-snapshots),如Force re-download of release dependency using Maven中所述
IntelliJ菜单中的
.m2 (请参见Clear the Maven Cache),然后在IntelliJ中重新加载/重新导入.m2。https://stackoverflow.com/questions/69966958
复制相似问题