简体   繁体   English

Spring:无法导入库

[英]Spring: cannot import library

i am new to Spring and went out to learn Spring tutorials from here using Eclipse.我是 Spring 的新手,从这里开始使用 Eclipse 学习 Spring 教程。

But, in Eclipse i cannot import these two libraries但是,在 Eclipse 中我无法导入这两个库

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

I even downloaded the Spring source files from the Spring site, included it in the system library, added it in my project and also in the lib folder of my project but no in vain.我什至从 Spring 站点下载了 Spring 源文件,将其包含在系统库中,将其添加到我的项目以及我的项目的 lib 文件夹中,但没有白费。

Can you help me out to know where exactly is the problem and suggest me some good Spring tutorials sites please.你能帮我弄清楚问题到底出在哪里,并请给我推荐一些好的 Spring 教程网站。

Thank you all谢谢你们

EDIT编辑

Here is the exception这是例外

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)

Finally got it. 终于明白了。 My mistake, i was using jars from the src folder and making it as a user library but the correct way is to use the jars from the dist folder. 我的错误,我使用src文件夹中的jar并将其作为用户库,但正确的方法是使用dist文件夹中的jar。

Thank you guys for your answers. 谢谢你们的回答。 :) :)

Based on comments: 基于评论:

Your jars haven't been included in the classpath. 您的罐子没有包含在类路径中。 Hence you're getting the compilation errors. 因此,您将收到编译错误。

  1. Go to project explorer perspective in eclipse 在eclipse中转到项目资源管理器视角
  2. right click on lib folder > Build path > Add to build path. 右键单击lib文件夹>构建路径>添加到构建路径。

This will include all jars inside the lib folder to your classpath and you won't get any compilation errors. 这将包括lib文件夹中所有jar类到你的类路径,你不会得到任何编译错误。

By the way. 顺便说说。 These classes are part of spring-context jar so make sure that's in the lib folder. 这些类是spring-context jar的一部分,所以请确保它位于lib文件夹中。

Update dependency in your pom.xml file, as below: 更新pom.xml文件中的依赖项,如下所示:

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

Add the dependency of spring-context.添加spring-context的依赖。 For gradle project.对于 gradle 项目。 implementation group: 'org.springframework', name: 'spring-context', version: '5.3.6'实现组:'org.springframework',名称:'spring-context',版本:'5.3.6'

this solved the issue for me.这为我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM