2016-09-26 38 views
3

我學習春天從本教程:的ApplicationContext進口春天

http://courses.caveofprogramming.com/courses/the-java-spring-tutorial/lectures/38024

在本教程中,講師下載春天在3.2.3版本的依賴關係(彈簧豆,春天背景,彈簧芯) 。發佈。

然後寫入驗證碼:

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然後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最新版本的依賴關係不是必需的嗎?我剛從Spring開始,每個人都說我應該使用最新版本。

編輯3;

,我發現使用彈簧背景下4.1.1.RELEASE

+1

提供您所使用的確切的pom.xml。還要詳細描述問題,「導入不起作用」不夠具體。 – kryger

回答

0

您必須添加依賴

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

我擁有所有的依賴關係。我編輯我的帖子並添加了這個信息 –

0

這可能是與Eclipse的一個問題,特別是如果你使用的是唯一的解決辦法舊版本。當您添加新的依賴關係時,Eclipse曾經遇到Maven項目的問題。 enter image description here

PS:

您可以強制Eclipse來更新它通過右鍵點擊你的項目並選擇Maven->更新項目

enter image description here

後,你的項目應該編譯蠻好的Maven依賴:檢查current documentation長達最新設置的基於XML的應用程序上下文建立

+0

我的Eclipse版本:4.6.0(霓虹燈)。 「更新項目...」不起作用。我也刪除,然後導入現有的Maven項目沒有結果。當我使用4.1.1.RELEASE spring依賴關係版本時,程序工作。這將是一個巨大的錯誤? –

1

要麼添加這些罐子在你的類路徑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>