今さら人には聞けない di 入門
DESCRIPTION
今さら人には聞けない DI 入門. 2006.05.14 エスエムジー株式会社 小森 裕介( [email protected] ). はじめまして!. 名前: 小森 裕介 Blog : http://d.hatena.ne.jp/y-komori/ 所属: エスエムジー株式会社( http://www.smg.co.jp ) 主な仕事: Java による集中監視制御システム設計・開発 集中監視制御フレームワークの設計・開発 Web アプリケーションシステムの設計・開発 教育・各種執筆活動 「 JAVA PRESS 」 - PowerPoint PPT PresentationTRANSCRIPT
© The Seasar Foundation and the others 2006. all rights reserved.1
Seasar ConferenceSeasar Conference2006 Spring2006 Spring
今さら人には聞けない今さら人には聞けない DIDI 入門入門
2006.05.14
エスエムジー株式会社小森 裕介( [email protected] )
© The Seasar Project and the others 2006. all rights reserved.2Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
はじめまして!はじめまして!
• 名前:名前:小森 裕介• BlogBlog :: http://d.hatena.ne.jp/y-komori/• 所属:所属:エスエムジー株式会社( http://www.smg.co.j
p )• 主な仕事:主な仕事:
– Java による集中監視制御システム設計・開発– 集中監視制御フレームワークの設計・開発– Web アプリケーションシステムの設計・開発– 教育・各種執筆活動
• 「 JAVA PRESS 」• 「日経ソフトウェア」「とことん作って覚える ! Java 入門」連載• 「なぜ、あなたは Java でオブジェクト指向開発ができないのか」
• Seasar2Seasar2 とのかかわりとのかかわり– S2Container コミッタ( PropertyInterType )、 S2JMS コ
ミッタ
© The Seasar Project and the others 2006. all rights reserved.3Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
はじめにはじめに
• どうも巷では「 DI コンテナ」というのが流行っているらしい・・・
• 興味はあるけど、ゆっくり勉強する時間ないし・・・
• どうせ今の仕事で使わないから、後回し・・・
そんなあなたにそんなあなたに40分40分でDIコンテナのでDIコンテナの魅力魅力
お伝えします!お伝えします!
そんなあなたにそんなあなたに40分40分でDIコンテナのでDIコンテナの魅力魅力
お伝えします!お伝えします!
© The Seasar Project and the others 2006. all rights reserved.4Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
DIDI で何が楽になるのか?で何が楽になるのか?
• アプリケーションの変更が楽になる!• アプリケーションのテストが楽になる!• アプリケーションの再利用が楽になる!
生産性向上!生産性向上!
© The Seasar Project and the others 2006. all rights reserved.5Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
こんなコードにウンザリしていませんこんなコードにウンザリしていませんか?か?
public class TottemoTaihen { private DataSource ds; private Logger logger; private HitsuyouNaLogic logic;
public TottemoTaihen(HitsuyouNaLogic logic) { this.logic = logic;
// データソースの取得 Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQL");
// ロガーの取得 this.logger = LoggerFactory.getLogger(this.getClass()); } public void doLogic() { ・・・本来の処理・・・ }}
public class TottemoTaihen { private DataSource ds; private Logger logger; private HitsuyouNaLogic logic;
public TottemoTaihen(HitsuyouNaLogic logic) { this.logic = logic;
// データソースの取得 Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/MySQL");
// ロガーの取得 this.logger = LoggerFactory.getLogger(this.getClass()); } public void doLogic() { ・・・本来の処理・・・ }}
コンストラクタ渡し
JNDI で取得
Factory から取得
本当に書きたいのは
ここだけなのに・・・
HitsuyouNaLogic
LoggerDataSource
TottemoTaihen
たったたった3クラス3クラスのの依存でも・・・依存でも・・・
依存クラスの依存クラスの準備がとってもタイヘ準備がとってもタイヘ
ン!ン!
Before DI…Before DI…
© The Seasar Project and the others 2006. all rights reserved.6Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
DIDI ならとってもカンタン!ならとってもカンタン!
public class DIdeKantan { private DataSource ds; private Logger logger; private HitsuyouNaLogic logic; public void doLogic() { ・・・本来の処理・・・ }
public void setDataSource(DataSource dataSource){ this.dataSource = dataSource; }
public void setLogger(Logger logger) { this.logger = logger; }
public void setHitsuyouNaLogic(HitsuyouNaLogic logic) { this.logic = logic; }}
public class DIdeKantan { private DataSource ds; private Logger logger; private HitsuyouNaLogic logic; public void doLogic() { ・・・本来の処理・・・ }
public void setDataSource(DataSource dataSource){ this.dataSource = dataSource; }
public void setLogger(Logger logger) { this.logger = logger; }
public void setHitsuyouNaLogic(HitsuyouNaLogic logic) { this.logic = logic; }}
簡単な setter メソッドを
用意するだけで OK!
必要な処理のコーディングに
集中できる!
After DI !!After DI !!
© The Seasar Project and the others 2006. all rights reserved.7Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
DI ContainerDI Container
Before DI Before DI ・ ・ After DIAfter DI
TottemoTaihen
DataSource Logger
HitsuyouNaLogic
Before DI…Before DI… 必要なオブジェクトは自分で準備するのが常識!
だから、とってもタイヘン!
Creat
e!Create!
Create!
After DI !!After DI !!
DIdeKantan
DataSource Logger
HitsuyouNaLogic
Inje
ctio
n!
Inje
ctio
n! Injection!
Injection!
Injection!Injection!
必要なオブジェクトはDI Container が
用意( Injection )してくれる!
クラス本来の記述に専念できる!クラス本来の記述に専念できる!
© The Seasar Project and the others 2006. all rights reserved.8Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
寿司にたとえるDI-非寿司にたとえるDI-非 DIDI なちらしなちらし寿司寿司
非非 DIDI ななちらし寿司ちらし寿司•材料が多くて準備がタイヘン!•材料が揃えば混ぜるだけなのに・・・
まぐろ
錦糸卵
いくら
えびうににんじ
んしいた
け
public class ちらし寿司 { private 酢飯 sumeshi; private まぐろ maguro; private 錦糸卵 kinshiTamago; private いくら ikura; private えび ebi; private うに uni; private にんじん ninjin; private しいたけ shiitake;
public ちらし寿司 () { 酢飯準備 (); まぐろ準備 (); ・・・省略・・・ しいたけ準備 (); }
public class ちらし寿司 { private 酢飯 sumeshi; private まぐろ maguro; private 錦糸卵 kinshiTamago; private いくら ikura; private えび ebi; private うに uni; private にんじん ninjin; private しいたけ shiitake;
public ちらし寿司 () { 酢飯準備 (); まぐろ準備 (); ・・・省略・・・ しいたけ準備 (); }
public void まぜる () { 酢飯の上に材料をちらす } private void 酢飯準備 () { ・・・酢飯の準備・・・ }
private void まぐろ準備 () { ・・・まぐろの準備・・・ } ・・・省略・・・ private void しいたけ準備() { ・・・しいたけの準備・・・ }}
ソースコードにたとえれば・・・ソースコードにたとえれば・・・
© The Seasar Project and the others 2006. all rights reserved.9Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
寿司にたとえるDI-寿司にたとえるDI- DIDI な手巻き寿な手巻き寿司司
DIDI なな手巻き寿司手巻き寿司•寿司ネタは最初から用意されている•あとは好きなものを巻くだけ!
public class 手巻き寿司 { private 酢飯 sumeshi; private まぐろ maguro; private 厚焼き卵 atsuyakiTamago; private きゅうり kyuuri; private えび ebi; private いくら ikura;
public void 巻く () { お好きにどうぞ! }
public class 手巻き寿司 { private 酢飯 sumeshi; private まぐろ maguro; private 厚焼き卵 atsuyakiTamago; private きゅうり kyuuri; private えび ebi; private いくら ikura;
public void 巻く () { お好きにどうぞ! }
ソースコードにたとえれば・・・ソースコードにたとえれば・・・
※ setter の記述は省略しています「手巻き寿司」のつもり ↑
酢飯 まぐろ 厚焼き卵
きゅうり えび いくら
© The Seasar Project and the others 2006. all rights reserved.10Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
現在利用できる代表的な現在利用できる代表的な DIDI コンテコンテナナ
• Spring FrameworkSpring Framework– http://www.springframework.org/– 世界的に有名
• Seasar2Seasar2– http://www.seasar.org/– 世界的には、(今のところ)マイナー– 純国産のプロダクト– Spring には無い利点がいっぱい
© The Seasar Project and the others 2006. all rights reserved.11Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
DIDI コンテナは何をしてくれるのか?コンテナは何をしてくれるのか?
• 必要なクラスを DI コンテナに登録しておくと、コンテナから取り出すときに依存オブジェクトをセット( Injection )してくれる
DI ContainerDI Container
DIdeKantan
Logger
DataSource
HitsuyouNaLogic
LoggerDataSource
HitsuyouNaLogic
DIdeKantan
<components> <component class=“DIdeKantan” /> <component class=“DataSource” /> <component class=“Logger” /> <component class=“HitsuyouNaLogic” /></components>
<components> <component class=“DIdeKantan” /> <component class=“DataSource” /> <component class=“Logger” /> <component class=“HitsuyouNaLogic” /></components>
設定(設定( XMLXML )ファ)ファイルイル
登録登録
取得取得① 対象オブジェクトの生成② 依存オブジェクトの生成③ 依存オブジェクトのセット
がおこなわれる
オブジェクトの生成はコンテナまかせ!オブジェクトの生成はコンテナまかせ!
© The Seasar Project and the others 2006. all rights reserved.12Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
DIDI のコンテナのもう一つの利点-のコンテナのもう一つの利点-AOPAOP
• DI コンテナはオブジェクトを生成するだけでなく、オブジェクトに動的な機能追加もできる
設定(設定( XMLXML )ファイル)ファイル
登録登録
<components> <component class=“DIdeKantan”> <aspect class=“TraceInterceptor” /> </component> <component class=“HitsuyouNaLogic”> <aspect class=“TraceInterceptor” /> </component></components>
<components> <component class=“DIdeKantan”> <aspect class=“TraceInterceptor” /> </component> <component class=“HitsuyouNaLogic”> <aspect class=“TraceInterceptor” /> </component></components>
DI ContainerDI Container
DIdeKantan
HitsuyouNaLogic
DIdeKantan
ログ出力機能ログ出力機能
HitsuyouNaLogic
ログ出力機能ログ出力機能
横断的に実現したい機能を「アスペクト」として登録してお
く
取得取得コンテナから取得した時点で
機能が組み込まれている
•ソースコードの修正は一切不要!•自動生成などの面倒な作業もなし!
http://s2container.seasar.org/ja/aop.htmlMore Info!
© The Seasar Project and the others 2006. all rights reserved.13Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
「設計と実装の分離」の実現「設計と実装の分離」の実現
• DI の世界では「設計と実装の分離」が本当に本当に実現できる
DIdeKantan<<interface>>
HitsuyouNaLogic
HitsuyouNaLogicImpl
private HitsuyouNaLogic logic;・・・logic.foo();・・・
private HitsuyouNaLogic logic;・・・logic.foo();・・・
DI ContainerDI Container
<components> ・・・ <component class=“HitsuyouNaLogicImpl” /> ・・・</components>
<components> ・・・ <component class=“HitsuyouNaLogicImpl” /> ・・・</components>インターフェース
に依存
実装クラスを登録
<<interface>>
HitsuyouNaLogic
HitsuyouNaLogicImpl MockHitsuyouNaLogic
テスト用のモックオブジェクトを作った場合・・・テスト用のモックオブジェクトを作った場合・・・DI ContainerDI Container
<components> ・・・ <component class=“MockHitsuyouNaLogic” /> ・・・</components>
<components> ・・・ <component class=“MockHitsuyouNaLogic” /> ・・・</components> モックオブジェクトに
変更
依存元( DIdeKantan )のソースコードは修正不要!依存元( DIdeKantan )のソースコードは修正不要!
© The Seasar Project and the others 2006. all rights reserved.14Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
DI×AOPDI×AOP の威力の威力
• DIDI とと AOPAOP の組み合わせでできることの組み合わせでできること
– モックオブジェクトの提供– 各種プーリングの実現– トランザクションの実現– 分散オブジェクトの実現・・・ etc
設計と実装の分離横断的な機能追加
DI ContainerDI Container
テスト用モッククラス
元の実装クラス
元の実装クラス
リモートオブジェクトを呼び出すスタブ
各種アスペクト適用
利用側
利用する側からはインターフェースしか見えてい
ない
どんな実装クラスを用意するかは
コンテナの設定次第!
© The Seasar Project and the others 2006. all rights reserved.15Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
デモ:デモ: DIDI でジャンケン!でジャンケン!
• ジャンケンプログラムで DI の効果を体験しよう!
© The Seasar Project and the others 2006. all rights reserved.16Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
StrategyStrategy パターンで手の出し方を変パターンで手の出し方を変えるえる
Player
TacticsRandomTactics
StoneOnlyTactics
CyclicTactics
ComputerPlayer
ランダム
グーだけ
グー・チョキ・パー順番に
ジャンケンの手を決める処理を
Tactics へ委譲
実装クラスの変更をDI コンテナで行う
© The Seasar Project and the others 2006. all rights reserved.17Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
TacticsTactics のインジェクションのインジェクション
public class ComputerPlayer extends PlayerImpl { private Tactics tactics;
public int showHand() { return tactics.readTactics(); }
public void setTactics(Tactics tactics) { this.tactics = tactics; }}
public class ComputerPlayer extends PlayerImpl { private Tactics tactics;
public int showHand() { return tactics.readTactics(); }
public void setTactics(Tactics tactics) { this.tactics = tactics; }}
ComputerPlayerComputerPlayer クラスクラス
TacticsTactics インターフェースインターフェースpublic interface Tactics { public int readTactics();}
public interface Tactics { public int readTactics();}
S2Container が自動的にsetter を呼び出して Tactics
の実装クラスをセットしてくれ
る
Tactics の実装クラスを意識する必要なし!
© The Seasar Project and the others 2006. all rights reserved.18Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
DIコンテナに対する5つのギモDIコンテナに対する5つのギモンン
• 依存関係はどうやって判断するの?依存関係はどうやって判断するの?– Seasar2 なら、インターフェースに基づいて自動判断します
• 結局設定ファイルをたくさん書くのでは?結局設定ファイルをたくさん書くのでは?– Seasar2 なら、 AutoRegister でカンタンに登録!
• 設定ファイルのデバッグが大変?設定ファイルのデバッグが大変?– Kijimuna(キジムナ)で記述の誤りがチェックできます!
• 結局はリフレクションでしょ、遅くないの?結局はリフレクションでしょ、遅くないの?– 独自のキャッシュ機構で速度低下はほとんどありません– Seasar2 は Spring と比較して2~ 300 倍高速です
• SetterSetter を書くのが面倒くさい!を書くのが面倒くさい!– PropertyInterType で setter いらず!
http://s2container.seasar.org/ja/benchmark/20060412_seasar_vs_spring.pdfMore Info!
http://kijimuna.seasar.org/More Info!
http://s2container.seasar.org/ja/DIContainer.html#ComponentAutoRegisterMore Info!
http://s2container.seasar.org/ja/DIContainer.html#AutoBindingModeMore Info!
http://s2container.seasar.org/ja/aop.html#AOPInterTypeMore Info!
© The Seasar Project and the others 2006. all rights reserved.19Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
PropertyInterType PropertyInterType でで settersetter 不要!不要!
public class DIdeKantan { private DataSource ds; private Logger logger; private HitsuyouNaLogic logic; public void doLogic() { ・・・本来の処理・・・ }
public void setDataSource(DataSource dataSource){ this.dataSource = dataSource; }
public void setLogger(Logger logger) { this.logger = logger; }
public void setHitsuyouNaLogic(HitsuyouNaLogic logic) { this.logic = logic; }}
public class DIdeKantan { private DataSource ds; private Logger logger; private HitsuyouNaLogic logic; public void doLogic() { ・・・本来の処理・・・ }
public void setDataSource(DataSource dataSource){ this.dataSource = dataSource; }
public void setLogger(Logger logger) { this.logger = logger; }
public void setHitsuyouNaLogic(HitsuyouNaLogic logic) { this.logic = logic; }}
setter すら書きたくない!
public class DIdeKantan { @Propery DataSource ds;
@Property private Logger logger;
@Property private HitsuyouNaLogic logic;
public void doLogic() { ・・・本来の処理・・・ }}
public class DIdeKantan { @Propery DataSource ds;
@Property private Logger logger;
@Property private HitsuyouNaLogic logic;
public void doLogic() { ・・・本来の処理・・・ }}
アノテーションの記述のみで setter を
動的に生成!
※ PropertyInterType は S2Container 2.4.0 beta1 ~ 利用できます ※ S2Container 2.4 系では、 EJB3.0 対応のため、さらに便利なフィールドインジェクションが利用可能になる予定です
© The Seasar Project and the others 2006. all rights reserved.20Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
DIDI コンテナの誤った使い方コンテナの誤った使い方
• なんでもなんでも DIDI コンテナに放り込めばよい?コンテナに放り込めばよい?
– DI コンテナに登録するのは、 再利用可能な「コンポーネント」の単位– どんな単位で登録するかは、きちんとした設計が必要– Webアプリケーションならば、
DIベースの設計手法「 Goya 」を参考にするとよい
No!No!
『 WEB+DB PRESS vol.31 』 (技術評論社) 「 Goya で学ぶ DI ベースのシステム設計」More Info!
※ Goal Oriented Yielding Approach
© The Seasar Project and the others 2006. all rights reserved.21Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
DIDI コンテナの使いどころコンテナの使いどころ
• DI コンテナのメリットはなんとなくわかった・・・
でも、開発現場で
どう役立てればいいの?
まずは、 S2Container を 100% 生かして作られた周辺プロダクトを使ってください!
まずは、 S2Container を 100% 生かして作られた周辺プロダクトを使ってください!
DI コンテナの機能をフルに使い切って設計するのは、
結構ムズカシイ!
そこで・・・そこで・・・
© The Seasar Project and the others 2006. all rights reserved.22Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
S2S2 ファミリーを使ってみよう!ファミリーを使ってみよう!
S2ContainerS2AOP S2Unit S2Tx S2DBCP S2JDBC
S2Remoting
S2Axis S2RMI S2JMS
S2JCA
分散技術分散技術
S2Dao
S2HibernateO/RO/R マッピングマッピング
S2JSF(Teeda)
S2Struts
Mayaa
S2Flex
プレゼンテーションプレゼンテーション
Tuigwaa
アプリケーションアプリケーション
S2Buri
ビジネスロジックビジネスロジック Kijimuna
S2JSF Plugin
開発用ツール開発用ツール
S2Javelin
・・・他製品との連携を実現するもの
・・・開発中プロジェクト
A・・・本カンファレンスで取り上げられているもの
http://www.seasar.org/products.htmlMore Info!
代表的な代表的な S2S2 ファミリー(ファミリー( JavaJava ))
© The Seasar Project and the others 2006. all rights reserved.23Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
EJB3EJB3 への潮流への潮流
• 「アンチEJB」として始まったDIコンテナの流れ
• DI コンテナの考え方は、EJB3の仕様に取り込まれた
• Seasar2 の弱点は「非標準」であること
Seasar2 2.4 ではEJB3に対応予定!Seasar2 2.4 ではEJB3に対応予定!
© The Seasar Project and the others 2006. all rights reserved.24Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
Seasar2Seasar2 をさらに知りたい方へをさらに知りたい方へ
• 『『 SeasarSeasar 入門 ~はじめての入門 ~はじめての DI&AOPDI&AOP~』~』– 税込価格: 3,570円– 出版社:ソフトバンククリエイティブ– ISBN: 4797331968– 監修:ひが やすを– 著:須賀 幸次/木村 聡/西川 麗/高安 厚思/白井 博章/椎
野 峻輔/岡 薫/藤村 浩士 DI コンテナの基礎から各種 S2 ファミリーの使い方ま
で幅広く網羅
• SeasarSeasar の使い方に困ったら・・・の使い方に困ったら・・・– Seasar-user メーリングリスト
• https://www.seasar.org/mailman/listinfo/seasar-user
© The Seasar Project and the others 2006. all rights reserved.25Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
ご質問についてご質問について
スピーカーブースでお待ちしています気軽にお越しください
© The Seasar Project and the others 2006. all rights reserved.26Seasar ConferenceSeasar Conference
2006 Spring2006 Spring
ご静聴ありがとうございました