【问题标题】:ApplicationContext not being recognized even after adding Maven dependency即使添加了 Maven 依赖项,也无法识别 ApplicationContext
【发布时间】:2021-12-26 05:18:14
【问题描述】:

我开始了一门关于 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>

【问题讨论】:

  • 可能最好展示您的 pom.xml...您可能没有设置所有正确的 springboot 依赖项..

标签: spring-boot intellij-idea classpath maven-dependency


【解决方案1】:

您不需要显式添加spring-context 依赖项,因为它是spring-boot-starter 的传递依赖项。除此之外,这对我来说似乎很好,但我们可以做三件事:

  1. 检查您实际上是在导入 org.springframework.context.ApplicationContext 而不是其他一些类;
  2. 强制重新加载 IntelliJ 中的所有依赖项(右键单击您的项目 --> Maven --> 重新加载项目);
  3. 清除您的.m2 文件夹(或至少其中的org/springframework 子路径文件夹)。那里可能有一些损坏的 JAR。

【讨论】:

  • 本地 Maven 存储库(.m2 文件夹)中可能损坏的 Spring 依赖项位于子路径 org/springframework 中(Java 在包命名空间中的点分隔符被解析为文件中的子文件夹-system:. 在 MacOS/Linux 上变为 /,在 Windows 上变为反斜杠 \)。
  • 感谢@hc_dev 的提示。再次复制和粘贴导致我出错:D
【解决方案2】:

根据我假设的给定屏幕截图,您正在 IntelliJ 中使用 Maven 项目。

从类路径添加导入

尝试在你的main方法所在的java类中导入org.springframework.context.ApplicationContext类(如截图所示)。

通常 IntelliJ 应该在您的类路径中找到类 ApplicationContext。从您给定的 POM 和依赖项 spring-boot-starter 中,类路径应该包含该类。

见: ApplicationContext import in spring

重新导入 Maven 依赖项

作为João suggests 尝试reimporting 通过上下文操作对您的 POM 文件 (CTRL + SHIFT + O)。

见:

修复操作

如果上述方法均无效,则可能是您的 IntelliJ 项目配置不正确,或者 IntelliJ 和 Maven 之间出现了不同步。然后尝试以下修复操作(按下面列出的顺序):

  1. 从 IntelliJ 的 Maven 工具或终端运行 mvn clean install -U(请参阅 --update-snapshots),如中所述 Force re-download of release dependency using Maven

  2. 在 IntelliJ 的菜单中选择 Invalidate Caches / Restart... 以重新索引您的项目。如果 IntelliJ 和 Maven 不同步,这会有所帮助。

  3. 删除您的本地 Maven 存储库,通常是用户主文件夹中的(隐藏)文件夹 .m2(请参阅 Clear the Maven Cache)并在 IntelliJ 中重新加载/重新导入您的 POM。

【讨论】:

    猜你喜欢
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 2021-04-16
    • 1970-01-01
    • 2019-07-03
    • 2019-08-18
    相关资源
    最近更新 更多