What's New in CDI 2.0 (JSR 365)

What's New in CDI 2.0 (JSR 365)

Contexts and Dependency Injection has become an integral part of Java to leverage dependency injection and bean management aside of EJB. The current CDI 1.2 is widely used amongst Java Enterprise 7 applications. The Expert Group is working since Fall 2014 on the next version of the specification with the goal to present the final CDI 2.0 spec in Q2/2016, which will be part of Java Enterprise 8.

You will learn about some of the upcoming highlights such as asynchronous events or how to boot CDI with Java SE. These features are already part of the draft release. You will get an overview of the changes, the Java 8 support, and how to apply the new features in your application. This is the right session for you if you are already a CDI user or want to become one and those that are interested in the progress of JSR 365 to build first-citizen CDI applications.

C5f6e8dffbb19acf405198c8fb917337?s=128

Mark Paluch

March 08, 2016
Tweet

Transcript

  1. What's New in CDI 2.0 JSR 365

  2. Mark Paluch ‣Software Craftsman ‣Spring Data Engineer @ Pivotal ‣EG

    for CDI 2.0 (JSR365) ‣Open Source committer
  3. CDI Timeline Dec 2009 CDI 1.0 (Java EE 6) Jun

    2013 CDI 1.1 (Java EE 7) Apr 2014 CDI 1.2 (M R) Sep 2014 CDI 2.0 (Kickoff) June 2016 CDI 2.0 (Release)
  4. Parts

  5. CDI 2.0 Parts • CDI core • Java SE •

    Java EE Integration • Support implementations for two TCK modes
  6. Asynchronous Events

  7. Asynchronous Events • Notify event observers asynchronously • One or

    more different threads • Decoupled from synchronous events
  8. Firing Async Events @Inject
 private Event<MyEventPayload> event;
 public void triggerEvent()

    {
 event.fireAsync(new MyEventPayload());
 }
  9. Observing Async Events public void observer(@ObservesAsync MyEvent payload) {
 System.out.println("Yay,

    I'm called async!");
 }
  10. Asynchronous Events • Exceptions and Synchronization with CompletionStage • Active

    scopes: Request, Application • Custom scopes depend on the implementation
  11. Ordered Events

  12. Ordered Events • Add @Priority to event observers • Aids

    observer ordering • Total global order determined when the event is fired
  13. Observer Ordering public void observer(@Observes @Priority(2500) MyEvent event) {
 System.out.println("Default

    priority");
 }
  14. public void earlier(@Observes @Priority(2499) MyEvent event) {
 // yay! I'm

    first
 System.out.println("Notified before all other observers");
 }
 public void observer(@Observes MyEvent event) {
 System.out.println("Default priority");
 } public void later(@Observes @Priority(2501) MyEvent event) {
 System.out.println("Notified after all other observers");
 }
  15. Meta-Data Builder API • Standardized API • AnnotatedTypes, Beans, BeanAttributes,

    InjectionPoints, and ObserverMethods • Builder and Configurator-style
  16. public class MyExtension {
 public void afterBeanDiscovery( @Observes AfterBeanDiscovery event)

    {
 BeanBuilder<MyBean> builder = Builders.bean(MyBean.class); builder.configure().addQualifier(new TransactionalLiteral());
 Bean<MyClass> bean = builder.build();
 
 event.addBean(bean);
 } } Meta-Data Builder API
  17. Java SE • Bootstrap API • Start/stop contexts

  18. Bootstrap API public static void main(String[] args) {
 
 try(CDI<Object>

    cdi = CDI.current()) {
 cdi.select(MyApp.class).get().runMyApplication();
 }
 }
 
 public class MyApp{
 
 public void runMyApplication(){
 // ...
 }
 }
  19. What else is in work? • Java 8: Repeating annotations

    • Proxying of classes with final methods • AOP on produced and custom beans
  20. Get in touch Web: http://www.cdi-spec.org/ Mailing list: cdi-dev@lists.jboss.org IRC: irc://freenode.net/#cdi-dev

    Twitter: @cdispec Github: https://github.com/cdi-spec CDI 2.0 JCP page: http://jcp.org/en/jsr/summary?id=365
  21. Q&A