1前書き
この基本チュートリアルでは、Spring Frameworkを使って簡単なXMLベースのBean設定を行う方法を学びます。
2概要
pom.xml
にSpringのライブラリ依存関係を追加することから始めましょう。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
Springの依存関係の最新版はhttps://search.maven.org/classic/#search%7Cga%7C1%7Cg%3A%22org.springframework%22%20AND%20a%3A%22spring-context%22[ここに]。
3依存性注入 – 概要
[Dependency injection]は、オブジェクトの依存関係が外部のコンテナによって提供される手法です。
実際にビジネスロジックを処理するサービスに依存するアプリケーションクラスがあるとしましょう。
public class IndexApp {
private IService service;
//standard constructors/getters/setters
}
それでは、
IService
がインターフェイスであるとしましょう。
public interface IService {
public String serve();
}
このインタフェースは複数の実装を持つことができます。
1つの潜在的な実装を簡単に見てみましょう。
public class IndexService implements IService {
@Override
public String serve() {
return "Hello World";
}
}
ここで、
IndexApp
は、
IService
という名前の下位コンポーネントに依存する上位コンポーネントです。
本質的に、私たちは
IndexApp
を
IService
の特定の実装から切り離しています。
4依存性注入 – 実行中
依存関係を注入する方法を見てみましょう。
4.1. プロパティを使う
XMLベースの設定を使用して依存関係を結び付ける方法を見てみましょう。
<bean
id="indexService"
class="com.baeldung.di.spring.IndexService"/>
<bean
id="indexApp"
class="com.baeldung.di.spring.IndexApp" >
<property name="service" ref="indexService"/>
</bean>
ご覧のとおり、
IndexService
のインスタンスを作成し、それにidを割り当てています。デフォルトでは、Beanはシングルトンです。また、
IndexApp
のインスタンスを作成しています。
このBean内に、setterメソッドを使用して他のBeanをインジェクトします。
4.2. コンストラクタを使う
setterメソッドを介してBeanをインジェクトする代わりに、コンストラクタを使用して依存関係をインジェクトすることができます。
<bean
id="indexApp"
class="com.baeldung.di.spring.IndexApp">
<constructor-arg ref="indexService"/>
</bean>
4.3. 静的ファクトリの使用
工場から返された豆を注入することもできます。与えられた数に基づいて
IService
のインスタンスを返す単純なファクトリを作りましょう:
public class StaticServiceFactory {
public static IService getNumber(int number) {
//...
}
}
では、XMLベースの設定を使用して
IndexApp
にBeanをインジェクトするために、上記の実装を使用する方法を見てみましょう。
<bean id="messageService"
class="com.baeldung.di.spring.StaticServiceFactory"
factory-method="getService">
<constructor-arg value="1"/>
</bean>
<bean id="indexApp" class="com.baeldung.di.spring.IndexApp">
<property name="service" ref="messageService"/>
</bean>
上記の例では、
factory-method
を使用して静的な
getService
メソッドを呼び出して、
IndexApp.
に注入するid
messageService
のBeanを作成しています。
** 4.4. ファクトリメソッドを使う
与えられた数に基づいて
IService
のインスタンスを返すインスタンスファクトリを考えてみましょう。今回は、このメソッドは静的ではありません。
public class InstanceServiceFactory {
public IService getNumber(int number) {
//...
}
}
それでは、XML設定を使用して
IndexApp
にBeanをインジェクトするために上記の実装を使用する方法を見てみましょう。
<bean id="indexServiceFactory"
class="com.baeldung.di.spring.InstanceServiceFactory"/>
<bean id="messageService"
class="com.baeldung.di.spring.InstanceServiceFactory"
factory-method="getService" factory-bean="indexServiceFactory">
<constructor-arg value="1"/>
</bean>
<bean id="indexApp" class="com.baeldung.di.spring.IndexApp">
<property name="service" ref="messageService"/>
</bean>
上記の例では、
InstanceServiceFactory
のインスタンスに対して
factory-method
を使用して
getService
メソッドを呼び出して、
IndexApp
に挿入するid
messageService
を持つBeanを作成します。
5テスト中
これにより、コンフィグレーション済みBeanにアクセスできます。
@Test
public void whenGetBeans__returnsBean() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("...");
IndexApp indexApp = applicationContext.getBean("indexApp", IndexApp.class);
assertNotNull(indexApp);
}
6. 結論
このクイックチュートリアルでは、Spring Frameworkを使用してXMLベースの設定を使用して依存関係を注入する方法の例を示しました。
これらの例の実装はhttps://github.com/eugenp/tutorials/tree/master/spring-core[GitHubプロジェクト]にあります – これはMavenベースのプロジェクトです。そのまま実行します。