Spring-autowiring-byname
提供:Dev Guides
春の自動配線「byName」
このモードは、プロパティ名で自動配線を指定します。 Springコンテナは、XML設定ファイルで_auto-wire_属性が_byName_に設定されているBeanを調べます。 次に、構成ファイル内の同じ名前で定義されたBeanとそのプロパティを一致させ、配線しようとします。 一致するものが見つかると、それらのBeanを注入します。 それ以外の場合、Beanは配線されません。
たとえば、設定ファイルでBean定義が_byName_をautowireに設定し、_spellChecker_プロパティが含まれている(つまり、_setSpellChecker _(…)メソッドがある)場合、SpringCheckerという名前のBean定義を検索し、プロパティを設定します。 それでも、<property>タグを使用して残りのプロパティを配線できます。 次の例で概念を説明します。
動作するEclipse IDEを用意し、次の手順を実行してSpringアプリケーションを作成します。
Steps | Description |
---|---|
1 | Create a project with a name SpringExample and create a package com.finddevguides under the *src *folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create Java classes TextEditor, SpellChecker and MainApp under the com.finddevguides package. |
4 | Create Beans configuration file Beans.xml under the* src* folder. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
以下は TextEditor.java ファイルの内容です-
以下は、別の依存クラスファイル SpellChecker.java の内容です。
以下は MainApp.java ファイルの内容です-
以下は、通常の状態の構成ファイル Beans.xml です。
しかし、自動配線「byName」を使用する場合、XML構成ファイルは次のようになります-
ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行しましょう。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-
link:/cgi-bin/printpage.cgi [__印刷]