2012-02-07 2 views
0

私は単純なSpring 2.5のhelloワールドプロジェクト(Spring Recipesから)を3.0に変換しようとしています。初心者の方は、新しいプロジェクト用のSpring Template ProjectオプションからSimple Spring Utility Projectを作成しました。Spring 2.5プロジェクトを3.0に変換するには

ソースコードを3.0プロジェクトの "src/main/java/com.apress.springrecipes.hello"パッケージにコピーしました。そこでは、Bean設定ファイルは "src/main/resources/META-INF/spring/app-context.xml"にあります。しかし、2.5のhelloの世界では "beans.xml"というファイルが呼び出され、プロジェクトのルートパスに作成されます。 Mainクラスのコードは次のようになります。

public static void main(String[] args) { 
    ApplicationContext context = 
     new ClassPathXmlApplicationContext("beans.xml"); 

    HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld"); 
    helloWorld.hello(); 
} 

新しいapp-context.xmlを見つけるにはどうすればよいですか?

答えて

2

私はCLASSPATHsrc/main/resources/META-INF/springディレクトリを追加してから、このようなコードを変更したい:

public static void main(String[] args) { 
    ApplicationContext context = 
     new ClassPathXmlApplicationContext("app-context.xml"); // why would expect to find beans.xml? 

    HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld"); 
    helloWorld.hello(); 
} 

変更が春バージョンとは何の関係もないこと。 CLASSPATHが何であるか、Springがアプリケーション・コンテキストXMLファイルをロードする場所を理解する必要があります。

注釈を自動配線に活用する方法を理解しておく必要があります。

Spring 3.1のnew featuresについてお読みください。

Springアプリケーションのコンテキストを変更して、新しいバージョン3.0の.xsdと名前空間を指すようにする必要があります。

もちろん、3.1 JARが必要です。

+0

ありがとうございました。将来の参照のために、STSでクラスパスをこのようなものに設定したい場合は、[Run、Run Configurations]を選択してください。ユーザエントリをクリックして進め、外部フォルダを追加してから、設定ファイルに至るパスまでドリルダウンします。私の場合、/ SpringRecipesHelloWord/src/main/resources/META-INF/springでした。 –

関連する問題