【问题标题】:Spring: cannot import librarySpring:无法导入库
【发布时间】:2012-05-28 05:11:35
【问题描述】:

我是 Spring 新手,并使用 Eclipse 从here 学习 Spring 教程。

但是,在 Eclipse 中我无法导入这两个库

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

我什至从 Spring 站点下载了 Spring 源文件,将其包含在系统库中,将其添加到我的项目中以及我的项目的 lib 文件夹中,但没有白费。

你能帮我知道问题出在哪里吗,请给我推荐一些好的 Spring 教程网站。

谢谢大家

编辑

这里是例外

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    ApplicationContext cannot be resolved to a type
    ClassPathApplicationContext cannot be resolved to a type

    at com.test.App.main(App.java:12)

【问题讨论】:

  • 您是否包含了 jar 以及来源​​?这才是真正重要的。
  • 不能在eclipse中导入他们的类不是spring的问题。在您的项目中包含这些 jar,然后重新启动 eclipse。我建议阅读 Spring in Action 3.0。手动导入这两个类时是否遇到任何编译错误?
  • @PaulBellora 是的,我确实包含了 jars 文件,甚至重新启动但无济于事
  • @VarunAchar 是的,我确实遇到了编译错误
  • @sameer,前段时间我也遇到过类似的问题。我认为不值得费心摆弄这一切,并安装了 SpringSource 工具套件。

标签: java eclipse spring


【解决方案1】:

基于 cmets:

您的 jar 文件尚未包含在类路径中。因此,您会遇到编译错误。

  1. 在 Eclipse 中进入项目资源管理器视角
  2. 右键单击lib文件夹>构建路径>添加到构建路径。

这会将 lib 文件夹中的所有 jar 包含到您的类路径中,并且您不会收到任何编译错误。

顺便说一句。这些类是 spring-context jar 的一部分,因此请确保它位于 lib 文件夹中。

【讨论】:

  • 是的,我同意.. 但是 lib 文件夹还在构建路径上吗?它可能已从构建路径中删除
【解决方案2】:

终于明白了。我的错误,我使用 src 文件夹中的 jar 并将其作为用户库,但正确的方法是使用 dist 文件夹中的 jar。

谢谢你们的回答。 :)

【讨论】:

    【解决方案3】:

    添加spring-context的依赖。 对于 gradle 项目。

    implementation group: 'org.springframework', name: 'spring-context', version: '5.3.6'
    

    这解决了我的问题。

    【讨论】:

      【解决方案4】:

      在你的 pom.xml 文件中更新依赖,如下:

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>3.0.5</version>
      </dependency>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-25
        • 1970-01-01
        • 2018-04-29
        • 2018-03-26
        • 2022-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多