contexts and dependency injection(cdi)入門
DESCRIPTION
「Contexts and Dependency Injection(CDI)入門」 本セッションではJava EE 6で仲間に加わったCDIを取り上げます。DI(Dependency Injection)はJava EE 5でも利用できましたが利用は限定的でした。CDIは、DIをより汎用的にしたもので、アプリケーションのコンポーネント間を疎結合にすることができます。入門者向けにCDIの基本概念から使用方法を紹介します。皆様も、Java EEアプリケーションを拡張性の高い疎結合なアーキテクチャにしましょう! 日本オラクル Fusion Middleware事業統括本部 松林 晶TRANSCRIPT
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 1
WebLogic Server 勉強会 Context and Dependency Injection(CDI) 入門
2013年 6月20日
日本オラクル株式会社
Fusion Middleware事業統括本部 松林晶
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 2
Program Agenda
Context and Dependency Injection(CDI)
– CDIとは
– CDI 1.1 New Feature(Java EE 7)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 3
Context and Dependency Injection(CDI)
CDIとは Java EE 6の仕様の一つ
– Contexts:サーバ上の状態を表現するもの
– Dependency Injection:依存性の注入
概要
コンポネント間を疎結合にできる コードがよりシンプルになる
インスタンスのライフサイクル管理や
インスタンス(の依存関係)の注入をコンテナが引き受ける
(=プログラマが、クラスをnewしたり、setメソッドを呼び出す必要が減少)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 4
Context and Dependency Injection(CDI) 何ができるか
DI(依存性注入) インターセプト
インスタンスのライフサイクル管理 EL式からのアクセス
Bean
Bean HTTPリクエスト
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 5
Context and Dependency Injection(CDI)
CDIはJava EE 6に準拠したアプリケーションサーバで使用可能
– Oracle WebLogic Server 12c
アプリケーションでCDIを有効にするには、beans.xmlが必要
– WARファイルの場合
「WEB-INF/beans.xml」を配置
– beans.xmlは中身が空でもよい
(特に記述する必要がない場合)
CDIを使うには
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 6
Context and Dependency Injection(CDI)
DIの機能はJava EE 5 でも使用可能だが…
Java EE 5のDI
@ManagedBean public class SampleMg { @EJB private Logic lg; }
JSF EJB JPA 依存関係
@Stateful public class Logic { @PersistenceContext(unitName=“JPA”) private EntityManager em; }
<?xml version="1.0" encoding="UTF-8" ?> <persistence xxx> <persistence-unit name=“JPA"> …略
プレゼンテーション層 ビジネスロジック層 データアクセス層
依存関係
注入する種別によってアノテーションが異なる 注入先も限定的
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 7
Context and Dependency Injection(CDI)
CDIを使用したコーディング
Java EE 6のCDI
@Named @RequestScoped public class SampleMg { @Inject private Logic lg; }
JSF EJB JPA 依存関係
@Stateful public class Logic { @Inject private EntityManager em; }
<?xml version="1.0" encoding="UTF-8" ?> <persistence xxx> <persistence-unit name=“JPA"> …略
プレゼンテーション層 ビジネスロジック層 データアクセス層
依存関係
注入する種別が異なっても@Injectで注入可能 注入先も自由
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 8
Context and Dependency Injection(CDI)
注入先の型がインターフェースの場合
実装クラスのインスタンスを生成して注入
実装クラスが複数ある場合はエラー
Java EE 6のCDI:何を注入するか①
public class xxBean{ @Inject private BizLogic logic; }
public class BizLogicImpl implement BizLogic { }
public class xxBean{ @Inject private BizLogic logic; }
public class BizLogicImplJAPAN implement BizLogic { }
public class BizLogicImplUS implement BizLogic { }
Error
Qualifierの機能を使用して適切に注入できるようになる
public class xxBean{ @JPN @Inject private BizLogic logic; }
@JPN public class BizLogicImplJAPAN implement BizLogic { }
注入
注入
解決方法例
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 9
Context and Dependency Injection(CDI)
@Producesアノテーションを使用
– 動的に注入することも可能
Java EE 6のCDI:何を注入するか②
public class xxBean{ @Inject private BizLogic logic; }
public class BizLogicImplJAPAN implement BizLogic { }
public class BizLogicImplUS implement BizLogic { }
public class HelloGenerator { @Produces public BizLogic createBizLogic{ // 条件毎に分岐 if( xxx ){ return new BizLogicImplJAPAN(); } else if( yyy ){return new BizLogicImplUS(); }
注入
注入
or
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 10
ビルトインのスコープ一覧
Context and Dependency Injection(CDI)
# 種別 概要 イメージ
1 @RequestScoped
1回のHTTPリクエストの期間
2 @ApplicationScoped
アプリケーションの生存期間と同じ
3 @SessionScoped
HTTPセッションの期間
4 @ConversationScoped
1回のHTTPリクエストの期間
もしくは、任意の期間
5 @Dependent
(デフォルト)
バインドされたインスタンスに依存する
Beanのスコープ(1)
?
We
bアプリ
ケーションと
同じ
HTTPリクエスト&レスポンス
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 11
Context and Dependency Injection(CDI) Beanのスコープ(2)ConversationScoped
package beans; import java.io.Serializable; import javax.enterprise.context.Conversation; import javax.enterprise.context.ConversationScoped; import javax.inject.Inject; import javax.inject.Named; @Named @ConversationScoped public class ConverBean implements Serializable{ @Inject private Conversation conversation; public void begin(){ conversation.begin(); } public void end(){ conversation.end(); } }
インスタンス生成
Conversation#begin()
インスタンス破棄
Conversation#end() Conversation#setTim
eout()によるミリ秒で指定した時間の到来で破棄も可能
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 12
Context and Dependency Injection(CDI) Beanのスコープ(3) Dependent
バインド先のインスタンスのライフサイクルに合わせる
– 例えば、Servletの実装クラスにBeanを注入した場合
Beanのライフサイクルは、Servletのライフサイクルと同じとなる
xxxS
erv
let
xxxB
ean
xxxS
erv
let
@R
equest
Scoped
xxxB
ean
@R
equest
Scoped
xxxB
ean
@R
equest
Scoped
xxxB
ean
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 13
Context and Dependency Injection(CDI) EL式からのアクセス
JSFパッケージのManagedBeanをCDIのBeanに置き換え可能
– EL式からアクセスするには、@Namedアノテーションが必要
– EL式ではデフォルトでクラス名でアクセス
クラス名:TestBean ⇒ EL:testBean
<body>
<h:outputText value="#{testBean.msg}" />
</body>
xxxx.xhtml @RequestScoped public class TestBean {
@Named @RequestScoped public class TestBean {
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 14
CDI 1.1 New Feature (Java EE 7)
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 15
Batch Applications for the Java Platform Java EE 7 Release!!!(2013年6月12日)
Java EE 7
Concurrency
JSON API
WebSocket
JMS 2.0
JAX-RS 2.0
EL 3.0
Expanded CDI
& REST Support Broad Industry
Participation
Build on EE 6
Momentum
Batch
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 16
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. 17