java – 在春天导入ApplicationContext

我正在从本教程学习
Spring

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版本. Mabye最新版本的依赖是不是necesery?我刚刚开始使用Spring,每个人都说我应该使用最新版本.

编辑3;

我找到的唯一解决方案是使用spring-context 4.1.1.RELEASE

最佳答案 在类路径org.springframework.context.support-3.1.0.RELEASE.jar和org.springframework.context-3.1.0.RELEASE.jar中添加这些jar.

或者,如果您正在使用maven并更新项目,请添加此依赖项.

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>  
 <version>4.x.x.RELEASE</version>    
</dependency>
点赞