Spring中的ApplicationContext导入

5
我正在学习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导入不起作用。当我将其更改为旧版本时,它能正常工作。"不起作用"是指eclipse不知道在我写"ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");" 时应该导入什么,以及当我自己编写"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,每个人都说我应该使用最新版本。

编辑 3:

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


1
请包含您正在使用的确切pom.xml文件。此外,请详细描述问题,“导入不起作用”不够具体。 - kryger
4个回答

2

如果您的项目没有使用maven,那么请将这两个jar包(org.springframework.context.support-3.1.0.RELEASE.jar 和 org.springframework.context-3.1.0.RELEASE.jar)添加到您的类路径中。

如果您正在使用maven,请添加以下依赖项并更新项目:

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

0

你必须添加依赖项

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

我已经安装了所有依赖项。我编辑了我的帖子并添加了这些信息。 - Michal Ryzio

0
这可能是Eclipse的问题,特别是如果您使用的是旧版本。以前,Eclipse在添加新依赖项时会出现Maven项目的问题。
您可以通过右键单击项目并选择Maven->更新项目来强制Eclipse更新其Maven依赖项。

enter image description here

之后您的项目应该可以正常编译: 在此输入图像描述

附注:请查看当前文档以获取最新的基于XML的应用程序上下文设置


我的Eclipse版本是4.6.0(Neon)。 “更新项目…”无法工作。我还删除了现有的Maven项目,然后重新导入,但结果仍然没有改变。只有当我使用4.1.1.RELEASE版本的Spring依赖项时,程序才能正常运行。这样做会是一个巨大的错误吗? - Michal Ryzio

0

我在pom.xml文件中的dependencies标签内添加了这个。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.3.7</version>
    <type>pom</type>
</dependency>

它的作用是从中下载spring-context-5.3.7.jar文件,可以导入ApplicationContext类。 希望对您也有用。
以下是我的pom.xml文件,以便更好地参考。
<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.Sharma</groupId>
  <artifactId>NachoVarga</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>NachoVarga</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.7</version>
    <type>pom</type>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.3.7</version>
    <type>pom</type>
</dependency>

  </dependencies>
</project>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接