我正在从本教程学习 Spring:
http://courses.caveofprogramming.com/courses/the-java-spring-tutorial/lectures/38024 http://courses.caveofprogramming.com/courses/the-java-spring-tutorial/lectures/38024
在本教程中,讲师下载版本 3.2.3.RELEASE 中的 spring 依赖项(spring-beans、spring context、spring-core)。
然后写下这段代码:
package com.caveofprogramming.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
Person person = (Person)context.getBean("person");
person.speak();
}
}
当我在最新版本 4.3.3.RELEASE 中使用 spring-context、spring-beans 和 spring-core 时,ApplicationContext 导入不起作用。当我将其更改为旧版本时它可以工作。 “不起作用”意味着当我写“ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");”时,eclips不知道我应该导入什么。当我自己写“import org.springframework.context.ApplicationContext”时,它是下划线的。
我应该怎么做才能导入具有最新版本依赖项的 ApplicationContext?
编辑:
这是错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
ApplicationContext cannot be resolved to a type
FileSystemXmlApplicationContext cannot be resolved to a type
at com.caveofprogramming.spring.test.App.main(App.java:10)
这是我的 pom 文件:
<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.caveofprogramming.spring.test</groupId>
<artifactId>spring-tutorial-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
</dependencies>
</project>
编辑2:我还看到该程序接受4.1.1.RELEASE版本。也许不需要最新版本的依赖项?我刚刚开始使用 Spring,每个人都说我应该开发最新版本。
Edit 3;
我发现的唯一解决方案是使用 spring-context 4.1.1.RELEASE