Spring-setter-based-dependency-injection
Spring Setterベースの依存性注入
セッターベースのDIは、引数のないコンストラクターまたは引数のない静的ファクトリーメソッドを呼び出してBeanをインスタンス化した後に、コンテナーがBeanでセッターメソッドを呼び出すことによって実現されます。
例
次の例は、純粋なセッターベースのインジェクションを使用してのみ依存関係をインジェクトできるクラス_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;
//a setter method to inject the dependency.
public void setSpellChecker(SpellChecker spellChecker) {
System.out.println("Inside setSpellChecker." );
this.spellChecker = spellChecker;
}
//a getter method to return spellChecker
public SpellChecker getSpellChecker() {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
ここでは、セッターメソッドの命名規則を確認する必要があります。 変数 spellChecker を設定するには、Java POJOクラスに非常によく似た* setSpellChecker()メソッドを使用します。 別の依存クラスファイル *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"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.finddevguides.SpellChecker"></bean>
</beans>
コンストラクターベースのインジェクションとセッターベースのインジェクションで定義されたBeans.xmlファイルの違いに注意する必要があります。 唯一の違いは、コンストラクターベースの注入に<constructor-arg>タグを使用し、セッターベースの注入に<property>タグを使用した<bean>要素内です。
2番目に重要な点は、オブジェクトへの参照を渡す場合、<property>タグの ref 属性を使用する必要があり、 value を直接渡す場合は、value属性を使用する必要があるということです。
ソースおよびBean構成ファイルの作成が完了したら、アプリケーションを実行しましょう。 すべてがあなたのアプリケーションでうまくいけば、これは次のメッセージを印刷します-
Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.
p-namespaceを使用したXML設定
セッターメソッドが多数ある場合は、XML構成ファイルで p-namespace を使用すると便利です。 違いを確認しましょう-
<property>タグを持つ標準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">
<bean id = "john-classic" class = "com.example.Person">
<property name = "name" value = "John Doe"/>
<property name = "spouse" ref = "jane"/>
</bean>
<bean name = "jane" class = "com.example.Person">
<property name = "name" value = "John Doe"/>
</bean>
</beans>
上記のXML構成は、次のようにp-namespaceを使用して、よりクリーンな方法で書き直すことができます-
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:p = "http://www.springframework.org/schema/p"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id = "john-classic" class = "com.example.Person"
p:name = "John Doe"
p:spouse-ref = "jane"/>
</bean>
<bean name =" jane" class = "com.example.Person"
p:name = "John Doe"/>
</bean>
</beans>
ここでは、p-namespaceでプリミティブ値とオブジェクト参照を指定する際の違いに注意する必要があります。 -ref 部分は、これがストレート値ではなく、別のBeanへの参照であることを示しています。