Spring-constructor-based-dependency-injection
コンストラクターベースの依存性注入
コンストラクターベースのDIは、コンテナーが、それぞれが他のクラスへの依存関係を表すいくつかの引数を使用してクラスコンストラクターを呼び出すときに実現されます。
例
次の例は、コンストラクターの注入でのみ依存関係を注入できるクラス_TextEditor_を示しています。
動作する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;
public TextEditor(SpellChecker spellChecker) {
System.out.println("Inside TextEditor constructor." );
this.spellChecker = spellChecker;
}
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">
<constructor-arg ref = "spellChecker"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.finddevguides.SpellChecker"></bean>
</beans>
ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行しましょう。 すべてがあなたのアプリケーションでうまくいけば、それは次のメッセージを印刷します-
Inside SpellChecker constructor.
Inside TextEditor constructor.
Inside checkSpelling.
コンストラクター引数の解決
複数のパラメーターがある場合、コンストラクターに引数を渡すときにあいまいさがある場合があります。 このあいまいさを解決するために、Bean定義でコンストラクター引数が定義される順序は、それらの引数が適切なコンストラクターに提供される順序です。 次のクラスを考慮してください-
package x.y;
public class Foo {
public Foo(Bar bar, Baz baz) {
//...
}
}
次の構成は正常に動作します-
<beans>
<bean id = "foo" class = "x.y.Foo">
<constructor-arg ref = "bar"/>
<constructor-arg ref = "baz"/>
</bean>
<bean id = "bar" class = "x.y.Bar"/>
<bean id = "baz" class = "x.y.Baz"/>
</beans>
コンストラクタに異なる型を渡すもう1つのケースを確認しましょう。 次のクラスを考慮してください-
package x.y;
public class Foo {
public Foo(int year, String name) {
//...
}
}
type属性を使用してコンストラクター引数の型を明示的に指定した場合、コンテナーは単純型との型マッチングも使用できます。 たとえば-
<beans>
<bean id = "exampleBean" class = "examples.ExampleBean">
<constructor-arg type = "int" value = "2001"/>
<constructor-arg type = "java.lang.String" value = "Zara"/>
</bean>
</beans>
最後に、コンストラクター引数を渡す最良の方法は、index属性を使用して、コンストラクター引数のインデックスを明示的に指定します。 ここでは、インデックスは0ベースです。 たとえば-
<beans>
<bean id = "exampleBean" class = "examples.ExampleBean">
<constructor-arg index = "0" value = "2001"/>
<constructor-arg index = "1" value = "Zara"/>
</bean>
</beans>
最後に、オブジェクトへの参照を渡す場合、<constructor-arg>タグの ref 属性を使用する必要があります。値を直接渡す場合は、上記のように value 属性を使用する必要があります。