Contenu connexe Similaire à CDI2.0アップデート&クックブック #JavaDayTokyo #jdt2016_4c (20) Plus de Norito Agetsuma (9) CDI2.0アップデート&クックブック #JavaDayTokyo #jdt2016_4c2. 上妻 宜人 あげつま のりと
• SIer技術部門でJavaトラブルシューティングの日々
• Java EEについて調べて伝えることが好き
• コミュニティ講演
• GlassFish.JP, JJUG CCC, Java Day Tokyo 2015 など
11. @ApplicationScoped%
public class Controller {%
%
%
}%
%
%
@SessionScoped%
public class ShoppingCart implements Cart {...}%
%
Scope
• @RequestScoped
• @SessionScoped
• @Applica0onScoped
• @Conversa0onScoped
• @Dependent
Context and Dependency Injec0on:
CDI管理Beanのスコープ
17. CDI1.1の変更点 (Java EE 7)
beans.xmlのオプション化
• @Priority
• 主にインターセプタ有効化 & 優先順位付けに使用
• Interceptorの有効化にbeans.xmlが不要に
@Interceptor%
@Priority(Interceptor.Priority.APPLICATION)%
public class LoggingInterceptor { %
@AroundInvoke%
public Object log(InvocationContext ic) %
throws Exception {...}%
}
23. @ApplicationScoped%
public class AlertService {%
@Inject AlertRepository repo;%
%
@Transactinal%
public void handleAlert(Alert alert) {%
// 何らかのビジネスロジックを終えた後に ...%
repo.persist(alert);%
}%
}
通常のメソッド呼び出し
最初はシンプルな機能
Alert
Service
Alert
Repository
24. @ApplicationScoped%
public class AlertService {%
@Inject AlertRepository repo;%
@Inject EmailSender email;%
%
@Transactinal%
public void handleAlert(Alert alert) {%
// 何らかのビジネスロジックを終えた後に ...%
repo.persist(alert);%
email.send(“test@test.com”, alert);%
}%
}
通常のメソッド呼び出し
アラートが来たらメールも ...
Alert
Service
Alert
Repository
Email
Sender
25. @ApplicationScoped%
public class AlertService {%
@Inject AlertRepository repo;%
@Inject EmailSender email;%
@Inject AlertCache cache;%
%
@Transactinal%
public void handleAlert(Alert alert) {%
// 何らかのビジネスロジックを終えた後に ...%
repo.persist(alert);%
email.send(“test@test.com”, alert);%
cache.putIfAbsent(alert);%
}%
}
通常のメソッド呼び出し
アラートが来たらキャッシュ更新も ...
Alert
Service
Alert
Repository
Email
Sender
Alert
Cache
54. public static void main(String ... args) {%
try (CDI<Object> cdi = CDI.getCDIProvider().initialize()) {%
Service service = cdi.select(Service.class).get();%
service.doSomething(); %
}%
}%
%
public class ServiceImpl implements Service {%
@Inject %
Repository repo;%
%
public void doSomething() {...}%
}%
CDI2.0 Java SE Support
Java SE Bootstrap API
CDIコンテナ初期化
InjectされたBeanの取得