2012-09-25 31 views
3

我正面臨着集成Eclipse RCP和Spring IOC的一些問題。Eclipse RCP找不到applicationContext.xml

以下是我對過程的處理方法。

步驟我做

  1. 創建一個Bundle(使用插件從exisitng檔案項目類型),其中有所有的Spring罐子。
  2. 創建一個帶有視圖的簡單Hello RCP應用程序。
  3. 新增的捆綁作爲一個依賴於RCP項目(第二步)

創建了一個簡單的類在我的RCP項目,其對象必須通過的applicationContext.xml來實例化。

public class Triangle { 
      public void draw(){ 
       System.out.println("Traingle drawn"); 
      } 
     } 

我的applicationContext.xml代碼

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> 
<beans> 
    <bean id="JGID" class="st.Triangle"/> 
</beans> 

我的觀點在那裏我取的applicationContext.xml的代碼段部分如下

​​

,這將引發錯誤

找不到類[st.Triangle]名稱'JGID'在 文件[D:\ applicationContext.xml]中定義;嵌套的例外是 拋出java.lang.ClassNotFoundException:st.Triangle

如何解決這個問題?

作爲一種變通方法我試過其他的方式,也是我在失敗下面即,使用的ClassPathXmlApplicationContext

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 

下面是錯誤

!MESSAGE無法創建視圖ID ST。查看:IOException從類路徑資源解析XML文檔[applicationContext.xml];嵌套異常是java.io.FileNotFoundException:類路徑資源 [applicationContext.xml]無法打開,因爲它不存在 !STACK 0 java.io.FileNotFoundException:類路徑資源[applicationContext.xml]無法打開

上面的代碼行在我的Eclipse RCP applcication中,它檢查或查找xml文件的位置。

我嘗試了以下方法。

  • 在src文件夾中放置applicationContext.xml。
  • 在項目文件夾中。
  • Inisde包。

在所有的3種情況下,它表示FileNotFoundException。我應該在哪裏放置applicationContext.xml文件以使applicationContext參考找到它?

+1

是'st.Triangle'正確的路徑? –

+0

@Jesse。是的,它是 – srk

回答

1

你確定三角是「CTX」類路徑?

可以instantinate例如

<bean id="str" class="java.lang.String" value="Hello World">

和另一個簡單的bean,然後調用

String str= (String) ctx.getBean("str");

+0

我通過設置日食夥伴策略解決了問題 – srk

1

將在您的應用程序的類路徑中spring-context.xml並以調用它,你需要下面的代碼

ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-context.xml"); 
      Triangle triangle = (Triangle) ctx.getBean("jgid"); 
      triangle.draw(); 

被盜從This site