java 无法访问 org.springframework.context.ConfigurableApplicationContext 类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38854465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:50:28  来源:igfitidea点击:

cannot access org.springframework.context.ConfigurableApplicationContext class file

javamavenintellij-ideaspring-boot

提问by I.shL

I am new to spring boot, while trying to build my first spring boot project (Maven) in intellij using spring initializer, I am getting this error when I run my project after creating it:

我是 spring boot 的新手,在尝试使用 spring 初始化程序在 intellij 中构建我的第一个 spring boot 项目 (Maven) 时,在创建项目后运行我的项目时出现此错误:

cannot access org.springframework.context.ConfigurableApplicationContext class file for org.springframework.context.ConfigurableApplicationContext not found

找不到 org.springframework.context.ConfigurableApplicationContext 无法访问 org.springframework.context.ConfigurableApplicationContext 类文件

I followed many tutorials but every time I am getting this error, how to resolve this?

我遵循了很多教程,但每次出现此错误时,如何解决?

My pom.xml file:

我的 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>com.boot</groupId>
    <artifactId>das-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>First spring boot app</name>
    <description>project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

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


</project>    

回答by Bilal Ahmed Yaseen

I was able to fix it by the following steps:

我能够通过以下步骤修复它:

  1. File -> Invalidate Cache and Restart
  2. File -> New -> Project from existing source
  3. Select the pom.xmlfile just (not the whole project directory) to load the project. Project will be setup automatically.
  1. File -> Invalidate Cache and Restart
  2. 文件 -> 新建 -> 从现有源项目
  3. 只选择pom.xml文件(不是整个项目目录)来加载项目。项目将自动设置。

回答by Suneet Srivastava

What worked me is run

对我有用的是运行

mvn package 

or

或者

mvn dependency:purge-local-repository    

and try invalidating the cache/restart the intellij idea.. Hope that helps

并尝试使缓存无效/重新启动 Intellij 想法.. 希望有所帮助

回答by Amit

I am using IntelliJ Idea, if I used "Import project", I faced the same issue. Even after using File -> Invalidate Cache and Restart issue was not resolved.

我正在使用 IntelliJ Idea,如果我使用“导入项目”,我会遇到同样的问题。即使使用 File -> Invalidate Cache and Restart 问题也没有解决。

I closed the project, removed it from the list.

我关闭了该项目,将其从列表中删除。

Then used "Open" instead of the "Import Project" button to import to the project. It worked fine. Hope it helps someone.

然后使用“打开”而不是“导入项目”按钮导入到项目中。它工作得很好。希望它可以帮助某人。

回答by Dan Lee

I am using IntelliJand gradlefor my spring boot application and encountered the same issue.

I resolved it by deleting the auto-generated .imlfile under classpath.

Hope it helps anyone who comes after.

我在Spring Boot 应用程序中使用IntelliJgradle,但遇到了同样的问题。

我通过删除类路径下自动生成的.iml文件来解决它。

希望对后来者有所帮助。

回答by David Enoma

  1. As the first answer stated, you can try invalidating caches and restarting. I tried that but it didn't work for me. I think it is an IntelliJ problem however.
  2. Check your maven dependencies if there is an error with the packages.
  3. I re-imported all my maven dependencies and also checked for the synchronization of version numbers in the pom.xml file. This was able to solve my own problem.
  1. 正如第一个答案所述,您可以尝试使缓存无效并重新启动。我试过了,但它对我不起作用。然而,我认为这是一个 IntelliJ 问题。
  2. 如果包有错误,请检查您的 Maven 依赖项。
  3. 我重新导入了我所有的 maven 依赖项,并检查了 pom.xml 文件中版本号的同步。这能够解决我自己的问题。

回答by Evol Rof

For me ,first answer only works when open the child project as a new project in a new window .

对我来说,只有在新窗口中将子项目作为新项目打开时,第一个答案才有效。

You can try delete main.imlor xxx.imlin your child project src\main\main.iml. That works for me

您可以尝试删除main.imlxxx.iml在您的子项目中src\main\main.iml。这对我行得通

回答by gai-jin

My fix:

我的修复:

  • right click project/module -> Open Module Settings -> Modules
  • select only the correct modules
  • 右键单击项目/模块 -> 打开模块设置 -> 模块
  • 只选择正确的模块

回答by Razzle

I was seeing the same error in a tooltip when I hovered over the red-underlined SpringApplication.run call in the main method (Intellij 2019.3 community ed) although the build seemed to go ahead fine. The wavy red underlines annoyed me though.

当我将鼠标悬停在主方法(Intellij 2019.3 社区版)中红色下划线的 SpringApplication.run 调用上时,我在工具提示中看到了同样的错误,尽管构建似乎进行得很好。不过,波浪状的红色下划线让我很恼火。

None of the above solutions seemed to help.

上述解决方案似乎都没有帮助。

Finally I went to the Project Structure dialog and noticed two things that I fixed:

最后,我转到“项目结构”对话框,注意到我修复了两件事:

1) I had named the main module to teamTest, and worried that the 'Test' suffix might trip up some auto configuration behaviours, and

1) 我已经将主模块命名为 teamTest,并担心“Test”后缀可能会导致一些自动配置行为,并且

2) I had a somewhat asymmetric module structure (the teamTest module was rooted in the project root, the test module was rooted in the src/test subdir)

2)我有一个有点不对称的模块结构(teamTest模块植根于项目根目录,测试模块植根于src/test子目录)

I removed the existing teamTest main module and added a new one called teamDemo rooted in the src/main subdir, as a sibling to the existing test module. Then I went into the pom.xml file and renamed the artefact from teamTest to teamDemo in a couple places, as I saw a warning about the pom.xml possibly overriding this change in future.

我删除了现有的 teamTest 主模块,并添加了一个名为 teamDemo 的新模块,该模块位于 src/main 子目录中,作为现有测试模块的同级。然后我进入 pom.xml 文件并在几个地方将人工制品从 teamTest 重命名为 teamDemo,因为我看到关于 pom.xml 的警告可能会在未来覆盖此更改。

For good measure I also: - closed the project - removed it from the history - closed Intellij - ran mvn dependency:purge-local-repository in a gitbash window

为了更好的衡量,我还: - 关闭项目 - 从历史记录中删除它 - 关闭 Intellij - 在 gitbash 窗口中运行 mvn dependency:purge-local-repository

before running Intellij again and opening the project again.

在再次运行 Intellij 并再次打开项目之前。

Red wavy underlines were gone.

红色波浪下划线消失了。

Phew! Hope this helps someone else...

呼!希望这对其他人有帮助...