Java 导入 org.springframework 无法解决

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35293116/
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-08-11 16:31:00  来源:igfitidea点击:

Import org.springframework cannot be resolved

javaeclipsespringdependency-injection

提问by Pedro Gordo

I'm trying to create a simple HelloWorld application with Spring DI. I created a Java project and imported some tutorial classes, one of which is simple this:

我正在尝试使用 Spring DI 创建一个简单的 HelloWorld 应用程序。我创建了一个 Java 项目并导入了一些教程类,其中一个很简单:

package helloworld;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;

import java.io.FileInputStream;
import java.util.Properties;

public class HelloWorldSpring {

    public static void main(String[] args) throws Exception {

        // get the bean factory
        BeanFactory factory = getBeanFactory();

However I'm getting the following error: The import org.springframework cannot be resolved. I'm using Eclipse Spring Tool Suite 3.7.2, and I thought that by using this Eclipse version when I clicked "Fix project setup", it would add these dependencies for me. What am I doing wrong here? Do I need to add these dependencies manually even in STS? If yes, what's the correct way to do it?

但是我收到以下错误:The import org.springframework cannot be resolved. 我使用的是 Eclipse Spring Tool Suite 3.7.2,我认为当我单击“修复项目设置”时使用这个 Eclipse 版本,它会为我添加这些依赖项。我在这里做错了什么?即使在 STS 中,我是否需要手动添加这些依赖项?如果是,那么正确的做法是什么?

采纳答案by Andre Coetzee

If you are using maven you need to add this to your pom.xml

如果您使用的是 maven,则需要将其添加到 pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
</dependencies>

回答by Buyanaa FreeDom

I changed Dalston.SR4 to Edgware.SR3 in pom properties. It works like charm.

我在 pom 属性中将 Dalston.SR4 更改为 Edgware.SR3。它就像魅力一样。

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