Spring-qualifier-annotation
提供:Dev Guides
Spring @Qualifierアノテーション
同じタイプのBeanを複数作成し、そのうちの1つだけをプロパティにワイヤリングしたい場合があります。 このような場合、 @ Qualifier アノテーションと @ Autowired を使用して、どのBeanを配線するかを指定することで混乱を解消できます。 以下は、@ Qualifierアノテーションの使用方法を示す例です。
例
動作する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 Student, Profile 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. |
これが Student.java ファイルの内容です-
以下は MainApp.java ファイルの内容です。
次の構成ファイル Beans.xml の例を検討してください
ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行しましょう。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-