#author("2018-07-01T04:11:43+00:00","default:haikikyou","haikikyou")
#author("2018-07-01T11:37:07+00:00","default:haikikyou","haikikyou")
[[Spring]]

#contents

Inversion of Control (IoC) 原則の実装である''依存オブジェクト注入''について以下整理する。

* 動作確認のための準備 [#fef6b9ad]

SpringのIoCコンテナのベースは、&code(){org.springframework.beans};と&code(){org.springframework.context};パッケージから構成されている。動作確認しながら仕組みを理解するために、Eclipse等のIDEでMavenプロジェクトを作成し、以下の設定をpon.xmlに追加しておくと良い。


#geshi(xml){{{
<dependencies>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>5.0.7.RELEASE</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>5.0.7.RELEASE</version>
	</dependency>
</dependencies>
}}}

&label(warn){参考}; https://mvnrepository.com/artifact/org.springframework

* Dependency Injection [#n2d57aac]

''設定を利用から分離する''ための仕組みのこと((参考リンク:https://kakutani.com/trans/fowler/injection.html))。~
コンポーネントの依存関係をソースコードから排除し、外部の設定ファイルなどから依存関係を注入する。~
具体的に例を見たほうが分かりやすいと思うので、以下SpringのDIについてサンプルプログラムを通して見ていく。

** Spring FrameworkのDI [#u0e98610]

- &code(){org.springframework.context.ApplicationContext};がインスタンスの生成、構成や組み立ての管理をしてくれる。
- ApplicationContextは、インスタンスの生成や組み立てに必要な情報をメタデータ(XMLファイル、アノテーション、Javaプログラム)から得る。

&color(silver){JavaEEでは、以前XML地獄と言われていたので、アノテーションやJavapプログラムベースの記述の方がポピュラーなのか??};

まずは、イメージを掴むため、簡単なサンプルプログラムを見てみる。~
以下は、スタンドアローンでDIを利用する例。

&label(sample){サンプル};  ''スタンドアローンのDIの利用例''

#geshi{{{
- src/main/java
  |- app.MyDISample.java
  |- beans.MyBean.java
- src/main/resources
  |- services.xml 
}}}

&size(12){''services.xml ''};

#geshi(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.xsd">

	<!-- myBeanを利用するための設定 -->
	<bean id="myBean" class="beans.MyBean">
		<property name="name" value="taro" />
		<property name="age" value="20" />
	</bean>

</beans>
}}}

&size(12){''beans.MyBean.java''};


#geshi(java){{{
package beans;

public class MyBean {

    private String name;
    private int age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
}}}

&size(12){''app.MyDISample.java''};

#geshi(java){{{
package app;

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

import beans.MyBean;

public class MyDISample {

    public static void main(String... args) {
        // メタデータのロード
        ApplicationContext context = new ClassPathXmlApplicationContext("services.xml");
        // myBeanオブジェクトの取得
        MyBean bean = context.getBean("myBean", MyBean.class);
        System.out.println(bean.getName()); // => taro
        System.out.println(bean.getAge()); // => 20
    }
}
}}}


トップ   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
目次
ダブルクリックで閉じるTOP | 閉じる
GO TO TOP