Spring-autowiring-bytype
春の自動配線「byType」
このモードは、プロパティタイプごとに自動配線を指定します。 Springコンテナは、XML設定ファイルで_autowire_属性が_byType_に設定されているBeanを調べます。 次に、 type が設定ファイル内のBean名の1つと正確に一致する場合、プロパティの一致と配線を試みます。 一致するものが見つかると、それらのBeanを注入します。 それ以外の場合、Beanは配線されません。
たとえば、設定ファイルでBean定義がautowire _byType_に設定されており、_SpellChecker_タイプの_spellChecker_プロパティが含まれている場合、Springは_SpellChecker_という名前のBean定義を探し、それを使用してプロパティを設定します。 それでも、<property>タグを使用して残りのプロパティを配線できます。 次の例は、XML構成ファイルが変更されていることを除いて、上記の例と違いがないことがわかる概念を示しています。
動作する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 ファイルの内容です-
package com.finddevguides;
public class TextEditor {
private SpellChecker spellChecker;
private String name;
public void setSpellChecker( SpellChecker spellChecker ) {
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
以下は、別の依存クラスファイル SpellChecker.java の内容です-
package com.finddevguides;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
以下は MainApp.java ファイルの内容です-
package com.finddevguides;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
以下は通常の状態の設定ファイル Beans.xml です-
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id = "textEditor" class = "com.finddevguides.TextEditor">
<property name = "spellChecker" ref = "spellChecker"/>
<property name = "name" value = "Generic Text Editor"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.finddevguides.SpellChecker"></bean>
</beans>
しかし、自動配線「byType」を使用する場合、XML構成ファイルは次のようになります-
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id = "textEditor" class = "com.finddevguides.TextEditor" autowire = "byType">
<property name = "name" value = "Generic Text Editor"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "SpellChecker" class = "com.finddevguides.SpellChecker"></bean>
</beans>
ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行しましょう。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-
Inside SpellChecker constructor.
Inside checkSpelling.
link:/cgi-bin/printpage.cgi [__印刷]