3

Eclipse RCP と Spring IOC の統合に関するいくつかの問題に直面しています。

以下は、プロセスに対する私のアプローチです。

私が行った手順

  1. すべての Spring jar を含むバンドル (既存のアーカイブ プロジェクト タイプのプラグインを使用) を作成しました。
  2. ビューを使用してシンプルな Hello RCP アプリケーションを作成しました。
  3. バンドルを依存関係として RCP プロジェクトに追加しました (step2)

オブジェクトがapplicationContext.xmlを介してインスタンス化する必要がある私のRCPプロジェクトに単純なクラスを作成しました。

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 をフェッチしているビューのコード スニペット部分は以下のとおりです。

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:/applicationContext.xml");
Triangle triangle = (Triangle) applicationContext.getBean("JGID");
triangle.draw();

これはエラーをスローします

ファイル [D:\applicationContext.xml] で定義された「JGID」という名前の Bean のクラス [st.Triangle] が見つかりません。ネストされた例外は java.lang.ClassNotFoundException: st.Triangle です

このエラーを解決するにはどうすればよいですか?

回避策として、別の方法を試しましたが、以下のように失敗しました。つまり、 ClassPathXmlApplicationContext を使用しています

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

以下はエラーです

!MESSAGE ビュー ID st.view を作成できません: クラス パス リソース [applicationContext.xml] から XML ドキュメントを解析中に IOException が発生しました。ネストされた例外は java.io.FileNotFoundException: クラスパス リソース [applicationContext.xml] が存在しないため開けません!STACK 0 java.io.FileNotFoundException: クラスパス リソース [applicationContext.xml] を開けません

私のEclipse RCPアプリケーションの上記のコード行は、どこでxmlファイルをチェックまたは検索しますか。

以下の方法を試しました。

  • applicationContext.xml を src フォルダーに配置しました。
  • プロジェクトフォルダーにあります。
  • パッケージに入れます。

3つのケースすべてで、それは言うFileNotFoundException. ファイルを見つけるための参照を作成するには、applicationContext.xmlファイルをどこに配置すればよいですか?applicationContext

4

2 に答える 2

1

Triangle が「ctx」クラスパスにあると確信していますか?

たとえば、別の単純な Bean をインスタンス化できますか

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

そして電話する

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

于 2012-10-04T07:55:56.953 に答える
1

アプリケーションのクラスパスに配置し、spring-context.xmlそれを呼び出すには、次のコードが必要です

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

このサイトから盗んだもの

于 2012-10-04T07:38:45.387 に答える