Import org.springframework cannot be resolved

2020-06-30 04:14发布

问题:

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:

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?

回答1:

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

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


回答2:

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

<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>