Spring Framework Dependency Injection(依存性注入)とは


スポンサーリンク

Dependency Injection(依存性注入)とはなにか?

2つのクラスがあるとします。
class Aとclass Bです。
AがBに依存している場合とはどのような場合でしょうか。

public class A {
	public void someMethod() {
		B b = new B();
		b.anyMethod();
	}
}

このように、class Aの中でclass Bのメソッドが使われている場合、AはBのインスタンスを取得する必要があります。

Dependency Injectionとは、オブジェクトを作成して、そのオブジェクトを必要とするクラスに対して、必要なインスタンスを提供(=注入)してくれます。

この例だと、セッターやコンストラクタを通じて、class Aに、class BのインスタンスをSpring Frameworkが注入してくれます。

public class A {
	private B b;
	public void someMethod() {
		//ここでBのインスタンスを作成する必要がなくなる
		b.anyMethod();
	}
	public void setB(B b) {
		this.b = b;
	}
}

なお、Springが管理するオブジェクトのことをbeanと呼びます。

サンプル

それでは、超簡単なサンプルを見てみましょう!
「見てみましょう」ってなんか教えてるみたいですが、俺も初心者です。
やってみます~。

まずはハマりがりなフォルダ構成ですが、eclipseのキャプチャを貼っておきます。

f:id:sho322:20140823104931j:plain
FriendというクラスをMainに注入するだけのサンプルです。

まずは、このFriend.javaを見てみます。

package sample1.bean;

public class Friend {
	private String firstName;
	private String lastName;
	private String interest;
	
	public Friend() {
	}
	
	public Friend(String firstName, String lastName, String interest) {
		this.firstName = firstName;
		this.lastName = lastName;
		this.interest = interest;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getInterest() {
		return interest;
	}

	public void setInterest(String interest) {
		this.interest = interest;
	}
	
}

このように、firstNameとlastNameとinterestという属性を持つクラスです。
こいつをinjectionします。

XMLを使ったinjectionです。
spring-config.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="friend1" class="sample1.bean.Friend">
		<property name="firstName" value ="takeshi" />
		<property name="lastName" value="yoshida" />
		<property name="interest" value="soccer" />
	</bean>
       
</beans>

property nameのところに、属性の名前を書いて、valueのところに値を入れます。
ここのbean id="XXXX"のXXXXを指定することで、ここに定義した値が入ったインスタンスを取得することができます。

では、Mainを見てみます。

package sample1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import sample1.bean.Friend;

public class Main {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring-config.xml"});
		Friend friend = context.getBean("friend1", Friend.class);
		System.out.println(friend.getLastName());
		System.out.println(friend.getFirstName());
		System.out.println(friend.getInterest());
	}
}

ClassPathXmlApplicationContextはクラスパス上にある定義ファイルを読み込むメソッドです。
複数指定することもできます。

実際に実行すると、コンソールに

yoshida
takeshi
soccer

と表示されます。

では次回以降に、コンストラクタを使ったinjectionなどのやり方を見ていきます。